这是有效的 PHP 语法吗?
if ($var == ($var1 || $var2))
{
...
}
我正在考虑使用它,但不确定它是否有效,而且似乎没有地方可以检查。
对我来说,这在逻辑上似乎是一致的,但我不确定,而且我没有可以在附近测试它的东西。
如果它是有效的,还有哪些其他主流语言支持这种构造。
编辑:比较是有效的,但不是我想的那样。
我想做的实际上是我刚刚发现的 in_array() 函数。
if ($var == ($var1 || $var2))
{
...
}
I am considering using this, but am ont sure if it is valid, and there doesn't seem to be somewhere to check.
It seems logically consistent to me but am not sure, and I don't have something to test it on close by.
If it is valid, what other mainstream languages support this sort of construct.
EDIT: The comparison is valid, but not in the way I was thinking.
What I was trying to do was actually the in_array() function, which I just discovered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的代码在语法上是有效的,但在语义上可能不是您想要的。
因为
$var1 || $var2
是一个布尔表达式并且总是产生true 或假。然后将$var
与该布尔表达式的结果进行比较。因此$var
始终与 true 或 false 进行比较,而不是与$var1
或$var2 进行比较
(这可能是您所期望的)。所以它不是($var == $var1) || 的简写($var == $var2)
。现在,正如您自己已经指出的那样,
in_array
是解决此问题的方法,如果您不这样做不想写像($var == $var1) || 这样的表达式($var == $var2)
,特别是当您想要比较任意数量的值时:这相当于:
如果您需要 严格比较(使用
===
而不是==
),将第三个参数设置为< em>true:现在相当于:
Your code is syntactical valid but semantical probably not what you wanted.
Because
$var1 || $var2
is a boolean expression and always yields true or false. And then$var
is compared to the result of that boolean expression. So$var
is always compared to either true or false and not to$var1
or$var2
(that’s what you’re have probably expected). So it’s not a shorthand to($var == $var1) || ($var == $var2)
.Now as you already noted yourself,
in_array
is a solution to this problem if you don’t want to write expressions like($var == $var1) || ($var == $var2)
, especially when you have an arbitrary number of values you want to compare to:Which is equivalent to:
If you need a strict comparison (using
===
rather than==
), set the third parameter to true:Which is now equivalent to:
是的,更正后的版本是有效的语法:
问题是,这是什么意思?
它将比较表达式
($var1 || $var2)
的结果(布尔值)与$var
的值。并且,如前所述,
php -l file.php
会告诉您是否存在任何语法错误。编辑:
考虑一下:
结果是:
Yes, the corrected version is valid syntax:
Question is, what does it mean?
It will compare the result of the expression
($var1 || $var2)
which will be a boolean, to the value of$var
.And, as mentioned,
php -l file.php
will tell you if there are any syntax errors.Edit:
Consider this:
Result is:
您可以从命令行使用命令 php -l filename.php 检查语法错误。
You can use the command php -l filename.php from the command line to check for syntax errors.
正如乔治·玛丽安所说,它缺少右括号,因此会引发语法错误。不过,它在其他方面是有效的,所以我看不出这是您不确定的逻辑 OR 构造本身。它被用于多种语言,包括 javascript。
As George Marian says, it's missing a closing parenthesis so would throw a syntax error. It's otherwise valid, though, so I can't see that it's the logical OR construct itself that you're unsure about. It's used in several languages, including javascript.
您更正的示例是有效的并且为 TRUE 如果 $var 为 TRUE 并且 $var1 或 $var2 为 TRUE .. OR 。如果 $var、$var1 和 $var2 均为 FALSE
your corrected example is valid and will be TRUE is $var is TRUE and either $var1 or $var2 is TRUE .. OR . if $var, $var1 and $var2 are all FALSE