Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identical match (with type checking) in switch statement PHP

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?

like image 624
Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ Avatar asked Jan 10 '16 06:01

Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ


People also ask

What will happen if there is a match in a switch statement?

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).

Can we use condition in switch case PHP?

PHP doesn't support this syntax. Only scalar values allowed for cases.

Which is faster if else or switch in PHP?

Due to the fact that "switch" does no comparison, it is slightly faster.

Do switch statements check every case?

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.

Is “match expression” the new switch statement in PHP 8?

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.

What is a SWITCH CASE statement in PHP?

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.

What is the difference between Switch and match in JavaScript?

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 (==).

Is pattern matching a game-changer for PHP?

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.


2 Answers

Try like this

$value = "";

switch (true) {
    case ($value === 0):
        echo "Zero";
        break;

    case ($value === 1):
        echo "One";
        break;

    case ($value === ""):
        echo "Empty";
        break;
}

DEMO

like image 72
Anik Islam Abhi Avatar answered Oct 15 '22 01:10

Anik Islam Abhi


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.

like image 36
jbafford Avatar answered Oct 15 '22 01:10

jbafford