note 105125 added to control-structures.switch
| From: | BasVijfwinkel at osu1 dot php dot net | Date: | Fri, 29 Jul 2011 01:03:30 +0000 |
| Subject: | note 105125 added to control-structures.switch | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181985@lists.php.net to get a copy of this message | ||
If you want to avoid problems with loose comparison and strings in case statements (0 matching the
first string case), you can use an explicit string cast in the switch statement:
switch((string)$switchkey) {...}
If $switchkey is 0 then the switch statement will either jump to the 'default' case or
execute nothing at all (if there is no 'default' case present).
<?php
$switchkey = 0;
if('a string' == $switchkey) echo 'a string is 0' . PHP_EOL;
if('a string' === $switchkey) echo 'but you will never see this' . PHP_EOL;
switch($switchkey)
{
case 'a string': echo 'switch string test without explicit
cast:'.$switchkey.' is a string (this is not what we want to see)' . PHP_EOL;break;
case 'another string': echo 'switch string test without explicit cast:
'.$switchkey.' is another string' . PHP_EOL;break;
default: echo 'switch string test without explicit cast: default :'.$switchkey.' is
somethign else (this is the correct choice)' . PHP_EOL;break;
}
switch((string)$switchkey)
{
case 'a string': echo 'switch string test with explicit
cast:'.$switchkey.' is a string (this is not what we want to see)' . PHP_EOL;break;
case 'another string': echo 'switch string test with explicit cast:
'.$switchkey.' is another string' . PHP_EOL;break;
default: echo 'switch string test with explicit cast: default :'.$switchkey.' is
something else (this is the correct choice)' . PHP_EOL;break;
}
$switchkey = true;
if('a string' == $switchkey) echo 'a string is TRUE' . PHP_EOL;
if('a string' === $switchkey) echo 'but you will never see this' . PHP_EOL;
switch($switchkey)
{
case 'a string': echo 'Switch boolean test without explicit
cast:'.$switchkey.' is a string (this is not what we want to see)' . PHP_EOL;break;
case 'another string': echo 'Switch boolean test without explicit cast:
'.$switchkey.' is another string' . PHP_EOL;break;
default : echo 'Switch boolean test : default without explicit cast: '.$switchkey.'
is something else (this is the correct choice)' . PHP_EOL;break;
}
switch((string)$switchkey)
{
case 'a string': echo 'Switch boolean test with explicit
cast:'.$switchkey.' is a string (this is not what we want to see)' . PHP_EOL;break;
case 'another string': echo 'Switch boolean test with explicit cast:
'.$switchkey.' is another string' . PHP_EOL;break;
default : echo 'Switch boolean test with explicit cast: default:s '.$switchkey.' is
something else (this is the correct choice)' . PHP_EOL;break;
}
?>
The script will output :
a string is 0
switch string test without explicit cast:0 is a string (this is not what we want to see)
switch string test with explicit cast: default :0 is something else (this is the correct choice)
a string is TRUE
Switch boolean test without explicit cast:1 is a string (this is not what we want to see)
Switch boolean test with explicit cast: default:s 1 is something else (this is the correct choice)
----
Server IP: 69.147.83.197
Probable Submitter: 180.12.129.221
----
Manual Page -- http://www.php.net/manual/en/control-structures.switch.php
Edit -- https://master.php.net/note/edit/105125
Del: integrated -- https://master.php.net/note/delete/105125/integrated
Del: useless -- https://master.php.net/note/delete/105125/useless
Del: bad code -- https://master.php.net/note/delete/105125/bad+code
Del: spam -- https://master.php.net/note/delete/105125/spam
Del: non-english -- https://master.php.net/note/delete/105125/non-english
Del: in docs -- https://master.php.net/note/delete/105125/in+docs
Del: other reasons-- https://master.php.net/note/delete/105125
Reject -- https://master.php.net/note/reject/105125
Search -- https://master.php.net/manage/user-notes.php