Javascript-PHP: $ a = $ aor $ B;

Source: Internet
Author: User
Tags php operator
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, $aIs null, false, -1All 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,$aIsnull,false,-1All 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 || bAlmost? 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 betrueSo you will find$aThe 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.

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.