这是有效的 PHP 语法吗?

发布于 2024-09-11 00:50:20 字数 244 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尾戒 2024-09-18 00:50:21

您的代码在语法上是有效的,但在语义上可能不是您想要的。

因为 $var1 || $var2 是一个布尔表达式并且总是产生true。然后将 $var 与该布尔表达式的结果进行比较。因此 $var 始终与 truefalse 进行比较,而不是与 $var1$var2 进行比较 (这可能是您所期望的)。所以它不是 ($var == $var1) || 的简写($var == $var2)

现在,正如您自己已经指出的那样, in_array 是解决此问题的方法,如果您不这样做不想写像 ($var == $var1) || 这样的表达式($var == $var2),特别是当您想要比较任意数量的值时:

in_array($var, array($var1, $var2))

这相当于:

($var == $var1) || ($var == $var2)

如果您需要 严格比较(使用===而不是==),将第三个参数设置为< em>true:

in_array($var, array($var1, $var2), true)

现在相当于:

($var === $var1) || ($var === $var2)

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:

in_array($var, array($var1, $var2))

Which is equivalent to:

($var == $var1) || ($var == $var2)

If you need a strict comparison (using === rather than ==), set the third parameter to true:

in_array($var, array($var1, $var2), true)

Which is now equivalent to:

($var === $var1) || ($var === $var2)
东风软 2024-09-18 00:50:21

是的,更正后的版本是有效的语法:

if ($var == ($var1 || $var2))

问题是,这是什么意思?

它将比较表达式 ($var1 || $var2) 的结果(布尔值)与 $var 的值。

并且,如前所述,php -l file.php 会告诉您是否存在任何语法错误。

编辑:

考虑一下:

$var1 = 1;
$var2 = 2;

echo var_dump(($var1 || $var2));

结果是:

bool(true)

Yes, the corrected version is valid syntax:

if ($var == ($var1 || $var2))

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:

$var1 = 1;
$var2 = 2;

echo var_dump(($var1 || $var2));

Result is:

bool(true)
So尛奶瓶 2024-09-18 00:50:21

您可以从命令行使用命令 php -l filename.php 检查语法错误。

You can use the command php -l filename.php from the command line to check for syntax errors.

后eg是否自 2024-09-18 00:50:21

正如乔治·玛丽安所说,它缺少右括号,因此会引发语法错误。不过,它在其他方面是有效的,所以我看不出这是您不确定的逻辑 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.

就此别过 2024-09-18 00:50:21

您更正的示例是有效的并且为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文