doupo1908 2012-12-04 21:08
浏览 6
已采纳

为什么PHP中的大于3

I am wondering why following statement in PHP is returning true?

true>=4

for example such line will echo 1

echo true>=4;

Can anyone explain me the logic behind this?

  • 写回答

2条回答 默认 最新

  • doufei8691 2012-12-04 21:20
    关注

    In addition to Davids answer, I thought to add something to give a little more depth.

    PHP unlike other programming languages, if your not careful with your operators/syntax you can fall into tricky pot holes like the one you experience.

    As David said,

    4 is also true (because it's non-zero), and true is equal to true, so it's also greater than or equal to true.

    Take this into account True is greater than false.

    true = 1

    false = 0

    So take this for example:

    $test = 1;
    if ($test == true){
    echo "This is true"; 
    }else{
    echo "This is false";
    }
    

    The above will output

    This is true

    But if you take this:

    $test = 1;
    if ($test === true){
    echo "This is true"; 
    }else{
    echo "This is false";
    }
    

    The above will output:

    This is false

    The added equals sign, looks for an exact match, thus looking for the integer 1 instead of PHP reading 1 as true.

    I know this is a little off topic, but just wanted to explain some pot holes which PHP contains.

    I hope this is some help

    Edit:

    In response to your question:

    echo true>=4;

    Reason you are seeing 1 as output, is because true/false is interpreted as numbers (see above)

    Regardless if your doing echo true>=4 or just echo true; php puts true as 1 and false as 0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源