dsgwdigu84950 2016-11-16 08:17
浏览 67
已采纳

带有数字PHP的if语句中的逻辑

Can someone explain what's happening here:

if(2 && 5 < 4)

If i've got for example

$x = 2 && 3;

and var_dump($x) it gives boolean(true) no matter what numbers are. But here it looks like numbers are comparing to 4 one by one.

  • 写回答

1条回答 默认 最新

  • duanqiao2225 2016-11-16 08:30
    关注

    Look at the PHP comparison table for PHP http://php.net/manual/en/types.comparisons.php

    For integers a number other than 0 returns true in comparison.

    if (2 && 5 < 4) => if (true && false) => false
    
    $x = 2 && 3 = 1 && 1 = 1
    

    because if a variable has an integer value, true becomes 1 because of type conversion.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?