在PHP中计算字符串和整数有什么区别?
我最近遇到了一个奇怪的问题在PHP中计算字符串和整数有什么区别?,php,string,int,Php,String,Int,我最近遇到了一个奇怪的问题 if($somestring != "" && $somestring > 0){ // do something. } 我的问题是,这两张支票是一样的吗?在PHP中,字符串在转换为INT时不总是大于0吗?或者字符串可以计算为0吗?也许这就是您要寻找的 “php”==0为真 “php”==0为false 也许这就是你想要的 “php”==0为真 “php”==0为false 可以有字符串“-1”,它将直接计算为-1 int 关于
if($somestring != "" && $somestring > 0){
// do something.
}
我的问题是,这两张支票是一样的吗?在PHP中,字符串在转换为INT时不总是大于0吗?或者字符串可以计算为0吗?也许这就是您要寻找的
为真“php”==0
为false“php”==0
为真“php”==0
为false“php”==0
- 也许这就是你想要的
关于您的状况-首先检查它是否为空-在这种情况下,它将被计算为0。看起来作者不希望php这样做,并且要求值第一个字符串可以为“空”,因此其中没有任何内容。 未设置任何内容时的整数,默认设置为0 所以细分是
if ($somestring != "") // 1 - check to see if the string is not empty
if ($somestring > 0) // 2 - Check to see of the non-empty string has a value greater than 0
因此,作者试图防止松散类型的脚本编写
希望这有帮助:)第一个字符串可以是“空”的,因此其中没有任何内容。 未设置任何内容时的整数,默认设置为0 所以细分是
if ($somestring != "") // 1 - check to see if the string is not empty
if ($somestring > 0) // 2 - Check to see of the non-empty string has a value greater than 0
因此,作者试图防止松散类型的脚本编写
希望这有帮助:)