dpf5207 2013-12-24 13:33
浏览 66
已采纳

为什么字符串大于或小于整数?

I am not able to get it that why the statement gets true both the times.?

$hello="foo";
     if($hello<=6){
        echo "yes
";
     }
     if ($hello>=0) {
        echo "ohh yess!";
     }

it outputs

yes
ohh yess!

I do know this is illegal comparison between integer and string,but why after all it gets true.

  • 写回答

3条回答 默认 最新

  • doupeng6890 2013-12-24 13:36
    关注

    As the PHP manual says:

    The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).

    In this case, the string foo evaluates to zero when the comparison is done and both the if conditions will hence evaluate to TRUE.

    Effectively, you'll be doing:

    if(0 <= 6) {
        echo "yes
    ";
    }
    
    if (0 >= 0) {
        echo "ohh yess!";
    }
    

    Use === operator if you want to make sure the variable types are also taken into consideration.

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

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭