Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a PHP switch with different types?

Tags:

php

How can I make the switch respect data types ( is there a workaround better then if/else ) ?

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

    Conclusion

  • Switch/case does loose comparison.
  • Solutions: switch with ifs or if/else
  • like image 748
    johnlemon Avatar asked Apr 06 '11 07:04

    johnlemon


    People also ask

    How does switch work in PHP?

    The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

    Can you nest switch statements PHP?

    To conclude, nested switch statements are possible and the way you used them in your code sample is correct.

    Can switch have two arguments?

    No. A switch statement lets you choose one case based on one value. Well, you could use a fall through and have several cases based on certain values...

    Which is better if-else or switch PHP?

    if-else better for boolean values: If-else conditional branches are great for variable conditions that result into a boolean, whereas switch statements are great for fixed data values. Speed: A switch statement might prove to be faster than ifs provided number of cases are good.


    2 Answers

    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';
    }
    
    like image 185
    KingCrunch Avatar answered Oct 21 '22 07:10

    KingCrunch


    i believe you can try if-then to facilitate the use of '===' instead:

    <?php
    $value = 0;
    
    if ($value==="") {
      echo "blank (string)";
    }
    else
    if ($value==="0") {
      echo "zero (string)";
    }
    else
    if ($value===false) {
      echo "false (boolean)";
    }
    else
    if ($value===null) {
      echo "null (object)";
    }
    else
    if ($value===0) {
      echo "zero (number)";
    }
    else {
      echo "other";
    }
    ?>
    
    like image 43
    nicola Avatar answered Oct 21 '22 07:10

    nicola