note 105125 deleted from control-structures.switch by cmb
| From: | cmb@php.net | Date: | Sun, 21 Apr 2019 11:53:05 +0000 |
| Subject: | note 105125 deleted from control-structures.switch by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210647@lists.php.net to get a copy of this message | ||
Note Submitter: Bas Vijfwinkel
----
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)