Php traps?

Source: Internet
Author: User
{Code...}: Why is this output?
$a=3;$b=6;$c=1;if($a=5||$b=7 && $c=10){    var_dump($a, $b,$c);//bool(true) int(6) int(1)    $a++;    $b++;}var_dump($a, $b,$c);//bool(true) int(7) int(1) 

Why is this output?

Reply content:
$a=3;$b=6;$c=1;if($a=5||$b=7 && $c=10){    var_dump($a, $b,$c);//bool(true) int(6) int(1)    $a++;    $b++;}var_dump($a, $b,$c);//bool(true) int(7) int(1) 

Why is this output?

Although your judgment is strange, I think it should be intentional. It should not be a writing BUG.

This is not a php bug, but you have not understood the priority of operators. We recommend you look at this:

  1. Operator priority
  2. Logical operators

Add brackets to your judgment, so you can understand this logical judgment.

$a = ( 5 || ( ( $b = 7 ) && ( $c = 10 ) ) )

You can add the brackets to the program for testing, and the results are the same. We can see from the above that=The priority is the lowest.( 5 || ( ( $b = 7 ) && ( $c = 10 ) ) ). Because||And&&The operator is of the same level, so operations are performed from left to right,5This number is determined by the programtrue. Why can I read the manual: convert it to a Boolean value?

AccordingOrOperating principles. Either of them is determined to betrueEnd the judgment immediately, soOrThe operation is not executed. FinallytrueAssigned$aVariable. That's why only$aThe variable value has changed, while$bAnd$cThe reason is not changed.

You can change the logic judgment to the following:

$a=0 || $b=7 && $c=10

What is the result?

Because0Determined by the programfalse. Or the right side of the operation is an operation, because it is two value assignment operations, so yestrue,fase or trueThe value is alsotrue, So$a=true,$b=7,$c=10

In addition, adding parentheses can increase the priority. If you want to achieve the purpose of copying, we can do this.

($ A = 0) | ($ B = 7) & ($ c = 10) // you can try ($ a = 5) | ($ B = 7) & ($ c = 10)

Supplement:

When you see the program subject, forget to read the following++In fact, we need to pay attention to the following:$a++This operation,++Is incremental, but why is the result stilltrueWhat about it? After reading the manual, we can find that, although very short, this sentence is very helpful to us:

Incremental or descending boolean values have no effect. -- Increment/decrease Operator

At this point, the truth of the program is revealed in the world. As to why PHP makes such a setting, it may be because PHP is written in C (Personal speculation, untrusted ).

So the program tells us one thing that we think is not what we get, so we should be good at doubting ourselves. Above!

If ($ a = 5 | $ B = 7 & $ c = 10)

If your judgment is incorrect, = is used for equality.

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.