使用条件运算符“?:”和“OR”的PHP语法惊喜

时间:2022-03-13 22:35:23

Today, I was open-mouthed by the following:

今天,我开口满口如下:

$asdf = ((1 OR true) ? "asdf" : "fdsa");
var_dump($asdf); // print "asdf"

$asdf = (1 OR true) ? "asdf" : "fdsa";
var_dump($asdf); // print "asdf"

$asdf = (1 OR true ? "asdf" : "fdsa");
var_dump($asdf); // print true

$asdf = 1 OR true ? "asdf" : "fdsa";
var_dump($asdf); // print 1

Ok, the last does not surprise me much, but the third? Can anyone explain?

好吧,最后一次并不让我感到惊讶,但第三次?谁有人解释一下?

3 个解决方案

#1


15  

This is all about operator precedence and their associativity

这完全是关于运算符优先级及其相关性的

http://php.net/manual/en/language.operators.precedence.php

http://php.net/manual/en/language.operators.precedence.php

or has lower precendence than = that is why it will be executed first

或者具有比=更低的优先级,这就是它首先被执行的原因

so $asdf = 1 OR true ? "asdf" : "fdsa";

所以$ asdf = 1还是真的? “asdf”:“fdsa”;

will be someting like

会有点像

($asdf = 1) or true ? :"asdf" : "fdsa" that is why it will print 1.

($ asdf = 1)还是真的? :“asdf”:“fdsa”这就是为什么它会打印1。

$a or $b check whether $a or $b is true if $a is true then it is returned and it does not even go to check $b

如果$ a为真,则$ a或$ b检查$ a或$ b是否为真然后返回它甚至不检查$ b

In third case

在第三种情况

$asdf = (1 OR true ? "asdf" : "fdsa");

$ asdf =(1 OR true?“asdf”:“fdsa”);

() has higher precedence than = so it will be executed before assignment.

()具有比=更高的优先级,因此它将在赋值之前执行。

To prove it

为了证明这一点

change OR to || which has higher precendence than =

将OR更改为||它具有比=更高的优先级

$asdf = 1 || true ? "asdf" : "fdsa";

var_dump($asdf); // print asdf

#2


3  

Here:

这里:

// use () - result in brackets assigned to $asdf
$asdf = (1 OR true ? "asdf" : "fdsa");
var_dump($asdf); // print true

And here:

和这里:

// = has higher precedence so $asfd equals 1 
// and it doesn't matter what is the result of ternary operator
$asdf = 1 OR true ? "asdf" : "fdsa";
// line equals to 
($asdf = 1) OR (true ? "asdf" : "fdsa");
// so $asdf is always 1 here
var_dump($asdf); // print 1

#3


2  

$asdf = (1 OR true ? "asdf" : "fdsa");

$ asdf =(1 OR true?“asdf”:“fdsa”);

It equals (1 OR (true ? "asdf" : "fdsa"));

它等于(1或(真?“asdf”:“fdsa”));

And it equals (1 OR "asdf");

它等于(1或“asdf”);

And this equals true;

这等于真的;

1 OR "asdf" is not equal (1 OR "asdf"). If you don't use brackets, the statement after OR operator is not important anymore. You assigned the first element as value. But if you use brackets, the first element will be statement in brackets

1或“asdf”不相等(1或“asdf”)。如果不使用括号,则OR运算符后的语句不再重要。您将第一个元素指定为值。但是如果你使用括号,第一个元素将是括号中的语句

#1


15  

This is all about operator precedence and their associativity

这完全是关于运算符优先级及其相关性的

http://php.net/manual/en/language.operators.precedence.php

http://php.net/manual/en/language.operators.precedence.php

or has lower precendence than = that is why it will be executed first

或者具有比=更低的优先级,这就是它首先被执行的原因

so $asdf = 1 OR true ? "asdf" : "fdsa";

所以$ asdf = 1还是真的? “asdf”:“fdsa”;

will be someting like

会有点像

($asdf = 1) or true ? :"asdf" : "fdsa" that is why it will print 1.

($ asdf = 1)还是真的? :“asdf”:“fdsa”这就是为什么它会打印1。

$a or $b check whether $a or $b is true if $a is true then it is returned and it does not even go to check $b

如果$ a为真,则$ a或$ b检查$ a或$ b是否为真然后返回它甚至不检查$ b

In third case

在第三种情况

$asdf = (1 OR true ? "asdf" : "fdsa");

$ asdf =(1 OR true?“asdf”:“fdsa”);

() has higher precedence than = so it will be executed before assignment.

()具有比=更高的优先级,因此它将在赋值之前执行。

To prove it

为了证明这一点

change OR to || which has higher precendence than =

将OR更改为||它具有比=更高的优先级

$asdf = 1 || true ? "asdf" : "fdsa";

var_dump($asdf); // print asdf

#2


3  

Here:

这里:

// use () - result in brackets assigned to $asdf
$asdf = (1 OR true ? "asdf" : "fdsa");
var_dump($asdf); // print true

And here:

和这里:

// = has higher precedence so $asfd equals 1 
// and it doesn't matter what is the result of ternary operator
$asdf = 1 OR true ? "asdf" : "fdsa";
// line equals to 
($asdf = 1) OR (true ? "asdf" : "fdsa");
// so $asdf is always 1 here
var_dump($asdf); // print 1

#3


2  

$asdf = (1 OR true ? "asdf" : "fdsa");

$ asdf =(1 OR true?“asdf”:“fdsa”);

It equals (1 OR (true ? "asdf" : "fdsa"));

它等于(1或(真?“asdf”:“fdsa”));

And it equals (1 OR "asdf");

它等于(1或“asdf”);

And this equals true;

这等于真的;

1 OR "asdf" is not equal (1 OR "asdf"). If you don't use brackets, the statement after OR operator is not important anymore. You assigned the first element as value. But if you use brackets, the first element will be statement in brackets

1或“asdf”不相等(1或“asdf”)。如果不使用括号,则OR运算符后的语句不再重要。您将第一个元素指定为值。但是如果你使用括号,第一个元素将是括号中的语句