note 129665 added to control-structures.switch

From: Date: Tue, 23 Jul 2024 08:20:17 +0000
Subject: note 129665 added to control-structures.switch
Groups: php.notes 
Request: Send a blank email to php-notes+get-221437@lists.php.net to get a copy of this message
It is possible to prevent nested switch/match/if blocks by checking for multiple cases at once (just beware that PHP uses loose comparison here). <?php $a = "abc"; $b = "def"; switch ([$a, $b]) { case ["abc", "def"]: $result = 1; break; default: $result = -1; } // $result == 1 ?> If for some cases one of the values is not important, you can use the variable itself: <?php $a = "abc"; $b = "def"; switch ([$a, $b]) { case ["xyz", "def"]: $result = 1; break; case [$a, "def"]: $result = 2; break; default: $result = -1; } // $result == 2 ?> ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 79.142.218.84) ---- Manual Page -- https://php.net/manual/en/control-structures.switch.php Edit -- https://main.php.net/note/edit/129665 Del: integrated -- https://main.php.net/note/delete/129665/integrated Del: useless -- https://main.php.net/note/delete/129665/useless Del: bad code -- https://main.php.net/note/delete/129665/bad+code Del: spam -- https://main.php.net/note/delete/129665/spam Del: non-english -- https://main.php.net/note/delete/129665/non-english Del: in docs -- https://main.php.net/note/delete/129665/in+docs Del: other reasons-- https://main.php.net/note/delete/129665 Reject -- https://main.php.net/note/reject/129665 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221437) next »