question regarding switch conditionals in php - php

I was hoping someone had a clue as to why I got the following outputs because I was expecting something else.
$x = NULL;
switch ($x) {
case "0":
echo "String";
break;
case 0:
echo "Integer";
break;
case NULL:
echo "NULL";
break;
case FALSE:
echo "Boolean";
break;
case "":
echo "Empty string";
break;
default:
echo "Something else";
break;
}
// outputs "Integer" was expecting "NULL"
$x=6;
switch ($x) {
case "6b":
echo "6b";
break;
case "6":
echo "6 empty";
break;
case 6:
echo "6 full";
break;
default:
echo "6 half";
break;
}
// outputs "6b" was expecting "6 empty"

The manual says:
Note that switch/case does loose comparision.
It will first check whether NULL == "0" which is false as NULL as a string is not "0".
NULL as integer is 0 so that will match - printing Integer.
For the 6, it will convert the cases to integers - "6b" will become 6 which is equal.
So it's because:
NULL != "0";
NULL == 0;
"6b" == 6;

Note:
Note that switch/case does loose comparision.
http://php.net/manual/en/control-structures.switch.php
So in stead of doing a comparison like: $x === 0 you do $x == 0

The comparison done by switch/case works with type jugling.
Basically, it uses the == operator, and not the === one.
Quoting Comparison Operators :
If you compare a number with a string or the comparison involves
numerical strings, then each string is converted to a number and the
comparison performed numerically. These rules also apply to the switch
statement.

$x is automatically converted to an integer when doing the comparison "$x = 0". You might want to use an if/else if structure instead, and use $x === 0 to do the comparison. === means "strictly equal to".

Related

Please explain why PHP switch case is always executing case 0 in this code

Can someone please explain why the case "a" is never reached in below code and why it will always execute case 0
switch ("a") {
case 0:
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
PHP, like JavaScript or Perl, is a loosely-typed language and will attempt to guess what you want to do. In this case, it changed your string to the closest integer it could find, which is zero. In other words, "a" == 0 is a true statement in PHP.
More on this topic can be found in the PHP documentation. I suggest you typecast the value in the switch statement, or replace it with an if/elseif/else construct.
As of PHP 8.0, this behaviour has changed and now the integer value will always be changed to a string before comparison between the two types. Strictly typing and comparing your variables remains the recommended practice, however.
You can not used mix-cases in a switch statement as PHP will interpret the meaning of what you mean.
In layman's terms, it will try to find the 'value of "a"' which is not defined to the processor, and hence is 0 in this case.
Same will go for the code below:
<?php
$x = "a";
switch($x)
{
case "c":
echo "c";
break;
case 1:
echo "1";
break;
case 0:
echo "0";
break;
case "a":
echo "a";
break;
case false:
echo "false";
break;
default:
echo "def";
break;
}
?>
Documentation is available at PHP.net
The reason for this is because switch uses a loose comparison ==
That said:
if ("a" == 0) // TRUE
if ("a" == true) // TRUE
Pretty much anything else will evaluate to false. (except "a" == "a")
So, if you have the need to compare against both strings and integers, you should just convert to string for the comparison.
//$var = "a";
$var = 0;
$var = strval($var);
switch ($var) {
case '0':
echo "0";
break;
case 'a':
echo "a";
break;
}
The variable type used on case() should be same type used in switch().
<?php
switch ("a") {
case "0":
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
For integer type:
<?php
switch (1) {
case 0:
echo 0;
break;
case 1: // never reached because "a" is already matched with 0
echo 1;
break;
}

How to use switch with integer 0 in PHP?

having the integer 0 as switch parameter will take the first result "foo":
$data=0; // $data is usually coming from somewhere else, set to 0 here to show the problem
switch ($data) :
case "anything":
echo "foo";
break;
case 0:
echo "zero";
break;
default:
echo "bar";
endswitch;
How do I change this, so the switch will write "zero" as expected?
The switch/case statement uses loose comparison, and, like it or not, 0 == "anything" is true:
Comparison Operators
[...] If you compare a number with a string or the comparison involves
numerical strings, then each string is converted to a number and the
comparison performed numerically. These rules also apply to the switch
statement. [...]
var_dump(0 == "a"); // 0 == 0 -> true
One solution is to change all case statements to string, and do a string comparison:
$data = 0;
switch ((string) $data): ## <- changed this
case "anything":
echo "foo";
break;
case "0": ## <- and this
echo "zero";
break;
default:
echo "bar";
endswitch;
Switch/case statement uses "loose-comparison" (i.e. ==. in this case, 0 also means false and 1 also means true. (http://www.php.net/manual/en/types.comparisons.php#types.comparisions-loose)
To avoid this problem, two solutions:
1) As suggested by #zzlalani, add quotes.
case '0': ...
2) Explicitly cast the switch statement to force a strict comparison (===)
switch((string)($data)) { ... }
Do like this
$data=0;
switch ($data)
{
case 0:
echo "bar";
break;
default:
echo "foo";
break;
}
EDIT :
How do I change this, so the switch will write "zero" as expected?
You need to move your case statement above.
$data=0;
switch ($data) :
case 0: // Moved this case to the begining
echo "zero";
break;
case "anything":
echo "foo";
break;
default:
echo "bar";
endswitch;
This is because switch does not do a "strict-type" checking.

PHP switch entering case 0: even if there is a valid case? [duplicate]

This question already has answers here:
How to handle a PHP switch with different types?
(5 answers)
Closed 9 years ago.
The following switch case statement was supposed to execute the value of case "january": but instead it is executing the value of case 0: ,can anybody tell me why it is doing this?
CODE:
<?php
$a = "january";
switch ($a)
{
case 0:
case 3:
echo "The value is either 0 or 3";
break;
echo "0/2";
break;
case "january":
echo "january";
break;
case "march":
echo "The value is ";
break;
case 2:
echo "The value is 2";
break;
default:
echo "Here is ur default message";
break;
}
?>
By the way it is doing the same for the case "march"....?
$a = 'january';
var_dump($a == 0);
var_dump($a === 0);
var_dump($a == '0');
var_dump($a === '0');
Output:
bool(true)
bool(false)
bool(false)
bool(false)
Read up on Type Juggling. Apparently switch uses loose comparison:
Note:
Note that switch/case does loose comparision.
PHP has some problems that can cause this kind of behaviour, in this case the problem comes from the fact that you are using different types on your Switch.
Try this code:
echo (0 == "january") ? "true\n" : "false\n"; // Echoes true
One way to work around this is to use only strings on your switch, like so:
switch ($a)
{
case "0":
case "3":
echo "The value is either 0 or 3";
break;
echo "0/2";
break;
case "january":
echo "january";
break;
case "march":
echo "The value is ";
break;
case "2":
echo "The value is 2";
break;
default:
echo "Here is ur default message";
break;
}
This will work fine, because 0 == "0" but "january" != "0"
Switch uses simple == to compare the values of the variable with the ones on each case.
You need to put quotes around the numbers. You can't mix numbers and strings when using a switch statement.

Why "switch(true){}" in php with a strange logic?

switch(false) {
case 'blogHitd':
echo('ddd');
break;
case false:
echo('bbbb');
break;
default:
echo 'alert("error action");';
}
-------output------
bbbb
switch(true) {
case 'blogHitd':
echo('ddd');
break;
case true:
echo('bbbb');
break;
default:
echo 'alert("error action");';
}
-------a strange output-------
ddd
Why, when I pass the value of true it will always select the first one?
From the PHP documentation on Booleans:
When converting to boolean, the following values are considered FALSE:
the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero
elements
an object with zero member variables (PHP 4 only)
the special
type NULL (including unset variables
SimpleXML objects created from
empty tags
Every other value is considered TRUE (including any resource).
The last sentence of this quoted passage is the line of interest in your case.
Switching "true" is only useful if you've got functions or variables in your "case" line
switch(true)
{
case is_array($array):
echo 'array';
break;
default:
echo 'something else';
break;
}
Note that switch/case does loose comparision.
http://www.php.net/manual/en/types.comparisons.php#types.comparisions-loose
PHP will typecast values for you, don't forget:
php > var_dump(true == 'bloghitd');
bool(true)
In this scenario the switch only runs the first valid case.
It is useful in the case that you have more than one possible answer but you want to run only the first one. For example:
switch(true){
case 1 == 2:
echo '1 == 2';
break;
case 2 == 2:
echo '2 == 2';
break;
case 3 == 3:
echo '3 == 3';
break;
case 4 == 1:
echo '4 == 1';
break;
}
The output:
2 == 2
Both the second and third cases are true, but we only get the second (which is the first TRUE).

PHP switch and case logical control

It possible to have logical control on case ?
ie:
$v = 0;
$s = 1;
switch($v)
{
case $s < $v:
// Do some operation
break;
case $s > $v:
// Do some other operation
break;
}
Is there a way to do something similar ?
The condition that is passed to switch is a value the cases are compared against. The condition (in your question $v) is evaluated once and then PHP seeks for the first case that matches the result.
From the manual (after Example #2), emphasis added:
The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement.
In your question switch ($v) is same as if you'd written: switch (0), because $v = 0. Then, your switch will try to find a case which equals to 0. And, just as #Kris said:
$s < $v evaluates to false, which triggers on the $v because it is 0 in here.
If you have to use conditions in case statements, your switch-condition should be a boolean, e.g.:
$v = 0;
$s = 1;
switch (true) {
case ($s < $v):
echo 's is smaller than v';
break;
case ($s > $v):
echo 's is bigger than v';
break;
}
Now your switch tries to seek the first case that evaluates to true, which in this case would be $s > $v.
Note that while the switch-condition is evaluated only once, cases are each evaluated in order:
$a = 1;
switch ($a) {
case 2:
echo 'two';
break;
case (++$a > 2):
break;
default:
echo $a;
}
echoes '2' from default-case, because when comparing $a to "2" $a was 1 and the case is discarded; while:
$a = 1;
switch ($a) {
case (++$a > 2):
break;
case 2:
echo 'two';
break;
default:
echo $a;
}
echoes 'two' from case '2' because ++$a > 2 increases $a but doesn't match $a.
default is a fallback and its position doesn't matter.
NB: the aforementioned switches are fugly and esoteric and are only provided as a proof-of-example.
Not the most readable use of, but yes you can.
Use if/else - if/elseif/else structure if you do need just a simple comparison.
This will not work, every case needs to be a scalar value. in your example case it is even worse, it may seem to work but...
$s < $v evaluates to false, which triggers on the $v because it is 0 in here.
http://php.net/manual/en/control-structures.elseif.php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}

Categories