note 84150 deleted from control-structures.switch by crell

From: Date: Mon, 27 Jun 2022 21:54:45 +0000
Subject: note 84150 deleted from control-structures.switch by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218651@lists.php.net to get a copy of this message
Note Submitter: mmxg at shaw dot ca ---- In reply to lko at netuse dot de Just so others know whom may not, that's because PHP does automatic type conversion if a string is evaluated as an integer (it sees the 2 in '2string' so when compared like if ('2string' == 2), PHP sees if (2 == 2) ). I just tested it, but if you go: <?php $string="2string"; switch($string) { case (string) 1: echo "this is 1"; break; case (string) 2: echo "this is 2"; break; case '2string': echo "this is a string"; break; } ?> The output will be "this is a string" and if you change $string to "2" it will again be "this is 2". Just in case that may help anyone who may run into that problem.

« previous php.notes (#218651) next »