【发布时间】:2013-07-14 19:14:16
【问题描述】:
我最近遇到了一个奇怪的问题。
if($somestring != "" && $somestring > 0){
// do something.
}
我的问题是,这两个检查是否相同?在 PHP 中转换为 INT 时字符串是否总是大于 0?或者一个字符串可以计算为 0?
【问题讨论】:
我最近遇到了一个奇怪的问题。
if($somestring != "" && $somestring > 0){
// do something.
}
我的问题是,这两个检查是否相同?在 PHP 中转换为 INT 时字符串是否总是大于 0?或者一个字符串可以计算为 0?
【问题讨论】:
可以有字符串“-1”,直接计算为-1 int
关于您的情况 - 首先检查它是否不为空 - 在这种情况下,它将被评估为 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
所以作者试图防止松散类型的脚本。
希望这会有所帮助:)
【讨论】: