Make Switch Use === Comparison Not == Comparison in PHP

make switch use === comparison not == comparison In PHP

Sorry, you cannot use a === comparison in a switch statement, since according to the switch() documentation:

Note that switch/case does loose comparison.

This means you'll have to come up with a workaround. From the loose comparisons table, you could make use of the fact that NULL == "0" is false by type casting:

<?php
$var = 0;
switch((string)$var)
{
case "" : echo 'a'; break; // This tests for NULL or empty string
default : echo 'b'; break; // Everything else, including zero
}
// Output: 'b'
?>

Live Demo

Using comparison operators in a PHP 'switch' statement

A more general case for solving this problem is:

switch (true) {
case $count <= 20:
$priority = 'low';
break;

case $count <= 40:
$priority = 'medium';
break;

case $count <= 60:
$priority = 'high';
break;

default:
$priority = 'severe';
break;
}

Switch doesn't work with numeric comparison cases

The value you pass into a switch statement is basically what the switch statement looks for an evaluated match for, going from top to bottom down the list of cases until it finds one it is equal to (loose comparison), e.g. true == true.

In your example, your comparisons are evaluated as booleans (true or false) - your variable $value is set to zero, which is equal to false, but not identical (strict comparison) to false. For example:

(0 == false)    // true
(0 === false) // false
(1 == false) // false
(1 === false) // false
(1 == true) // true
(1 === true) // false
(true === true) // true

So by using a boolean true as your switch value, you can do this to have numeric comparison inside the statement, where each comparison will evaluate to either true or false to match/not match the original true value (boolean - boolean comparison).

switch(true) {
case ($value <= 25): // true
$CompScore = 'low';
break;
case ($value > 25 && $value <= 50 ): // false
$CompScore = 'fair';
break;
case ($value > 50 && $value <= 75 ): // false
$CompScore = 'good';
break;
case ($value >75 ): // false
$CompScore = 'excellent';
break;
default: // if you removed the first case
$CompScore = 'low'; // this default case would be used
break;
}

php is / switch statement check with '0' string or '00' string

Well,
this is basically because php is not a strongly type language.
so "00" == "0" gives true (and as said switch uses the equality operator)

you might change this to if else statements with triple equals and that would do it.

"00" === "0" gives false. check this thread for more details. How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

How to use switch with integer 0 in PHP?

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;

How to handle a PHP switch with different types?

switch (true) {
case $value === '0' :
echo 'zero';
break;
case $value === '' :
echo 'empty';
break;
case $value === null :
echo 'null';
break;
case $value === false :
echo 'false';
break;
default :
echo 'default';
break;
}

I think, it's more readable than a if-elseif-chain like given below:

if ($value === '0') {
echo 'zero';
} else if ($value === '') {
echo 'empty';
} else if ($value === null) {
echo 'null';
} else if ($value === false) {
echo 'false';
} else {
echo 'default';
}

Switch statement and NULL versus 0

I was also going to suggest casting your switch input variable as something you can expect like (int) or (string) to make sure you're validating the cases properly.

PHP Switch Condition Catching Unmatched Condition

In order to compare the two, PHP has to convert "2" to a boolean (switch statements uses loose comparison, so values of different types must be cast to a single type). Anything that is non-zero is converted to "true."



Related Topics



Leave a reply



Submit