Because js often uses this method, but I try it in php but it doesn't work. $ a is null, false, and-1 is invalid. Now, I am using the code: {code ...} but how can this statement be met: {code ...} this is because js is often written in this way, but I cannot try it in php,
$a
Is
null
,
false
,
-1
All are invalid.
The method is as follows:
$a = $a?$a:$b;$a = is_*($a)?$a:$b;
But how can we meet the following requirements:
$a = $a or $b;
Reply content:
This is because js is often written in this way, but I cannot try it in php,$a
Isnull
,false
,-1
All are invalid.
The method is as follows:
$a = $a?$a:$b;$a = is_*($a)?$a:$b;
But how can we meet the following requirements:
$a = $a or $b;
PHP is not supported$a = $a || $b;
In this way, the return value is a Boolean value of the OR operation. (For priority, please read the answer from @ qinjianxiang)
However, for the ternary operations provided by PHP itself(expr1) ? (expr2) : (expr3)
SlavePHP5.3Expr2 can be omitted at the beginning, so you can abbreviated it as below:
$a = $a ?: $b;
The degree of conciseness should be consistent with that in JSa = a || b
Almost? Note: In PHP, 0, null, and ''(null string) are both = false.
Back to the annual PHP operator priority issue that had to be mentioned in January 1! Before answering this question, I had to link a previously answered question to the subject: PHP trap?
After reading the above link, you may understand that, in fact, the sentence you wrote in PHP won't achieve the desired effect at all, if you still don't understand it, I will add brackets to you:
($a = $a) or $b;
Read from left to right. The operation ratio or operation assigned by the value assignment takes priority. The last sentence is parsed into one or more operations, then there is an operation on the left that assigns itself a value to itself, which must betrue
So you will find$a
The value will never change.
The last question is that PHP does not seem to be able to be written in this form. if so, please let me know. thank you!
| In PHP and the or operator return values are Boolean values. Therefore, it is impossible to implement the effect of a = a | B in Javascript and Perl.
Which of the following statements is the most concise one? : Operator:
$a = $a ? $a : $b;
So what exactly does $ a = $ a or $ B return? Take a look at the test code:
It will output:
bool(false)
int(0)
Why is the output different? Because | and or operators have different priorities:
| The priority ratio is high. Therefore, $ a = $ a | $ B, equivalent to $ a = ($ a | $ B), which is $ a first, $ B evaluate or, get a boolean, and then assign this boolean value to $
Or has a lower priority than =. Therefore, $ a = $ a or $ B is equivalent to ($ a = $ a) or $ B. run $ a = $ a first, perform the or operation on the value of $ a and $ B, but the operation result is not assigned to any variable. Therefore, you cannot observe that the value of $ a is changed.
PHP operator priority: http://www.php.net/manual/en/language.operators.precedence.php
I forgot to fix my issue when I dug my grave. now I have to answer my questions. let's do seo for sf.
All the above are correct. now PHP7 is available.??
The operator is doing this.