javascript-php what can be written: $a = $a or $b;

Source: Internet
Author: User
Tags php operator
Because JS often have such a way of writing, but I tried in PHP but not, $aFor nullfalse-1is not valid at all.
Now the more verbose wording:

$a = $a?$a:$b;$a = is_*($a)?$a:$b;

But how to satisfy such a notation:

$a = $a or $b;

Reply content:

Because JS often have such a way of writing, but I tried in PHP but not, $a for null , false , -1 when all invalid.
Now the more verbose wording:

$a = $a?$a:$b;$a = is_*($a)?$a:$b;

But how to satisfy such a notation:

$a = $a or $b;

PHP does not support $a = $a || $b; this assignment, so the write returns a Boolean value of one or operation. (Priority level Please read @qinjianxiang students ' answers)

However, the ternary operation provided by PHP itself (expr1) ? (expr2) : (expr3) can be omitted from PHP5.3 beginning with expr2, so you can write it as follows:

$a = $a ?: $b;

How concise should be the same as JS inside a = a || b it? But note that PHP 0, NULL, ' (empty string) are ==false.

Back to the annual, once a month has to mention the PHP operator priority problem! Before answering this question, I have to link the question to the main topic: PHP traps?

Look at the link above you may understand, in fact, you write this sentence in PHP will not complete the effect you want, if you do not understand, I would like to add parentheses to you:

($a = $a) or $b;

How to understand it, from left to right reading, assignment operation than or operation precedence execution, finally this sentence is parsed into one or action, and then left is an assignment itself to the operation of itself, it is necessary for true so you will find $a the value will never change.

The last is a digression, PHP at present I know there seems to be no way to write this form should, if any, please tell me, thank you!

php | | and the OR operator return values are Boolean values, so a = A is impossible to implement like JavaScript and Perl | | B's effect.

The closest to the most concise writing is?: operator:

$a = $a ? $a : $b;

So what does a $ A = $a or $b return? And look at a test code:


  
   

它将输出:

bool(false)
int(0)

为什么输出不一样?因为 || 和 or 两个操作符的优先级不一样:

    • ||优先级比=高,所以,$a = $a || $b,等同于$a = ($a || $b),是先拿$a, $b求或,得到一个boolean,再把这个boolean赋值给$a
    • or优先级比=低,所以,$a = $a or $b,等同于($a = $a) or $b,先执行$a = $a,再把$a的值和$b执行or操作,但操作结果不赋值给任何变量,所以你观察不到$a的值被改变

PHP操作符优先级:http://www.php.net/manual/en/language.operators.precedence.php

挖坟的问题了,一直忘了结贴,现在自问自答,就当给sf做个seo吧.
楼上说的都对,现在PHP7开始有了??操作符就是做这个事情的.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.