isset 在旧版本中的工作方式是否有所不同
我得到了一些遗留代码,其中包含以下内容:
<?PHP
if(isset($_GET['pagina'])=="homepage") {
?>
HtmlCode1
<?php
} else {
?>
HtmlCode2
<?php
}
?>
我不知道确切的原因,但这似乎有效。 当我有 ?pagina=homepage 时加载 htmlcode1 ,当 pagina var 不存在或是其他东西时加载 htmlcode2 (还没有真正看到其他东西,只是不存在)。 该网站使用的是 php4(不知道确切的版本)。 但说实话,这怎么行呢? 我查看了手册,它说 isset 返回一个 bool ..
有人吗?
I got some legacy code that has this:
<?PHP
if(isset($_GET['pagina'])=="homepage") {
?>
HtmlCode1
<?php
} else {
?>
HtmlCode2
<?php
}
?>
I don't know exactly why but this seems to be working. The htmlcode1 is loaded when I have ?pagina=homepage and the htmlcode2 is loaded when the pagina var doesn't exist or is something else (haven't really seen with something else, just not there).
The website is using php4 (don't know the exact version).
But really, how can this work? I looked at the manual and it says isset returns a bool..
Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
isset() 返回 true 或 false。 在布尔比较中,
"homepage"
将计算为true
。 所以本质上你已经到了这里:如果 pagina 等于任何内容,你将看到 HtmlCode1。 如果没有设置,您将看到 HtmlCode2。
我只是尝试确认这一点,并且转到
?pagina=somethingelse
确实不显示 HtmlCode2。isset()
returns true or false. In a boolean comparison,"homepage"
would evaluate totrue
. So essentially you got here:If pagina equals anything, you will see HtmlCode1. If it is no set, you will see HtmlCode2.
I just tried it to confirm this, and going to
?pagina=somethingelse
does not show HtmlCode2.我怀疑这是一个错误,因为将真/假与“主页”进行比较并没有真正的意义。 我希望代码实际上应该是:
I suspect that it's a bug as it doesn't really make sense to compare true/false with "homepage". I would expect the code should actually be:
问题是“==”不是类型敏感的比较。 任何(非空)字符串都“等于”布尔 true,但与其相同(为此您需要使用“===”运算符)。
一个简单的例子,说明为什么您会看到这种行为:
http://codepad.org/aNh1ahu8
有关文档的更多详细信息,请参阅:
http://php.net/manual/en/language.operators.comparison。 php
https://www.php.net/manual/en/types.comparisons。 php (“与 == 表的松散比较”)
The problem is that "==" isn't a type-sensitive comparison. Any (non-empty) string is "equal" to boolean true, but not identical to it (for that you need to use the "===" operator).
A quick example, why you're seeing this behavior:
http://codepad.org/aNh1ahu8
And for more details about it from the documentation, see:
http://php.net/manual/en/language.operators.comparison.php
https://www.php.net/manual/en/types.comparisons.php (the "Loose comparisons with ==" table specifically)
这是如何工作的一些想法(除了前面提到的“主页”==true):
Some ideas how this could work (apart from the previously mentioned "homepage"==true):