Just found a strange situation where we need to run switch
statements only on identical match (with exact type). Just think about this scenario:
$value = "";
switch ($value) {
case 0:
echo "Zero";
break;
case 1:
echo "One";
break;
case "":
echo "Empty";
break;
default:
echo "None";
break;
}
This will echo
"Zero" where I want it to echo
"Empty". Is this possible to do in some way with switch
statement or I have the only way to use if...elseif...else
with ===
operator?
If the equality is found, switch starts to execute the code starting from the corresponding case , until the nearest break (or until the end of switch ). If no case is matched then the default code is executed (if it exists).
PHP doesn't support this syntax. Only scalar values allowed for cases.
Due to the fact that "switch" does no comparison, it is slightly faster.
A switch statement first evaluates its expression. It then looks for the first case clause whose expression evaluates to the same value as the result of the input expression (using the strict comparison, === ) and transfers control to that clause, executing all statements following that clause.
But it’s still sort of verbose and PHP 8 wants to change that using “Match expression”. An alternative for switch statement called “Match expression” has been accepted in this RFC. From the RFC… The RFC proposes adding a new match expression that is similar to switch but with safer semantics and the ability to return values.
Basically the switch statement is also known as a decision statement in PHP and other programming languages. The switch-case statement tests a variable against a range of values until it finds a match, and then executes a block of code corresponding to that match. Note:- The switch-case statement is an alternative to the if-elseif-else statement.
Similarly to a switch statement, a match expression has a subject expression that is compared against multiple alternatives. Unlike switch, it will evaluate to a value much like ternary expressions. Unlike switch, the comparison is an identity check (===) rather than a weak equality check (==).
However, the strictness of the match operator is appealing, and the perspective of pattern matching would be a game-changer for PHP. I admit I never wrote a switch statement in the past years because of its many quirks; quirks that match actually solve.
Try like this
$value = "";
switch (true) {
case ($value === 0):
echo "Zero";
break;
case ($value === 1):
echo "One";
break;
case ($value === ""):
echo "Empty";
break;
}
DEMO
PHP's switch statement is documented to use loose comparison. If you need exact matches, then unfortunately, the best bet is to use separate if/else tests with ===
.
One alternative is something similar to
switch(true) {
case $value === 0:
//...
break;
}
but that's about the same as a series of if-else statements, with more typing and indentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With