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

后端 未结 5 1506
醉酒成梦
醉酒成梦 2020-12-30 03:38
switch(false) {
    case \'blogHitd\':
        echo(\'ffffd\');
        break;
    case false:
        echo(\'bbbb\');
        break;
    default:
        echo \'alert         


        
5条回答
  •  -上瘾入骨i
    2020-12-30 04:23

    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.

提交回复
热议问题