【发布时间】:2012-09-26 04:52:54
【问题描述】:
关系运算符 ===(用于相同)是否可以与 != 运算符互换使用并获得相同的结果?或者我以后在执行更大的程序时最终会遇到问题吗?
我知道我会在下面的示例中得到相同的结果,这总是正确的吗?
//example 1
<?php
$a = 1; //integer
$b = '1'; //string
if ($a === $b) {
echo 'Values and types are same';
}
else {
echo 'Values and types are not same';
}
?>
// example 2
<?php
$a = 1; //integer
$b = '1'; //string
if ($a != $b) {
echo 'Values and types are not same';
}
else {
echo 'Values and types are same';
}
?>
【问题讨论】:
-
您为什么要这样做? PHP 的操作符已经够让人困惑了。
-
!= 不相等,您需要 !== 不相等且类型不同。 php.net/manual/en/language.operators.comparison.php
-
勾选这个问题你不会喜欢使用
==或!=,stackoverflow.com/questions/12598407/… -
我认为这在验证表单和检查用户是否具有特殊权限以查看基于特定数据库字段的特定页面时很有帮助。
标签: php