note 128212 added to control-structures.match

From: Date: Sun, 12 Feb 2023 14:46:07 +0000
Subject: note 128212 added to control-structures.match
Groups: php.notes 
Request: Send a blank email to php-notes+get-220168@lists.php.net to get a copy of this message
I use match instead of storing PDOStatement::rowCount() result and chaining if/elseif conditions or use the ugly switch/break : <?php $sql = <<<SQL INSERT INTO ... ON DUPLICATE KEY UPDATE ... SQL; $upkeep = $pdo->prepare($sql); $count_untouched = 0; $count_inserted = 0; $count_updated = 0; foreach ($data as $record) { $upkeep->execute($record); match ($upkeep->rowCount()) { 0 => $count_untouched++, 1 => $count_inserted++, 2 => $count_updated++, }; } echo "Untouched rows : {$count_untouched}\r\n"; echo "Inserted rows : {$count_inserted}\r\n"; echo "Updated rows : {$count_updated}\r\n"; ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.130 (proxied: 2a01:e0a:968:1840:b4f8:c796:4a84:4dc8) ---- Manual Page -- https://php.net/manual/en/control-structures.match.php Edit -- https://main.php.net/note/edit/128212 Del: integrated -- https://main.php.net/note/delete/128212/integrated Del: useless -- https://main.php.net/note/delete/128212/useless Del: bad code -- https://main.php.net/note/delete/128212/bad+code Del: spam -- https://main.php.net/note/delete/128212/spam Del: non-english -- https://main.php.net/note/delete/128212/non-english Del: in docs -- https://main.php.net/note/delete/128212/in+docs Del: other reasons-- https://main.php.net/note/delete/128212 Reject -- https://main.php.net/note/reject/128212 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220168) next »