drtkyykai004574380 2015-08-18 03:58
浏览 20

服务器需要PHP严格比较,但不需要在localhost上

I've run across a weird issue with comparison operators.

On my localhost this code runs fine:

$variable = 2;
if($variable == 2){
  echo 'hi';
}
if($variable == '2b'){
  echo 'bye';
}
//returns 'hi'

But on my server, it returns 'bye', unless I use the strict (===) operator.

Is there any way to change this so my server also returns 'hi'?

  • 写回答

2条回答 默认 最新

  • dongqian1028 2015-08-18 05:56
    关注

    This is because php try to cast (int) to your string because you compare an int with a string so your if block would be

    if($variable == (int) '2b'){
    

    So when casting to it, so (int) '2b' would be only 2. If you use === it works cause of, that is also check if it is the same type.

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)