PHP basic Trap question (variable assignment) _php tips

Source: Internet
Author: User

Copy Code code as follows:

<?php
$a = 3;
$b = 6;
if ($a =5| | $b =7) {
$a + +;
$b + +;
}
Var_dump ($a, $b);


Trap A

$a=5, $b = 7 as the $a==5, $b ==7
Error Result: 3,6

Trap Two

The precedence of the operator that the $a=5 assignment succeeded $b=7 not executed
Error Result: 6,7

Correct understanding

Traps are the precedence of operators, and the assignment operator (=) has the lowest precedence, so the correct understanding should be
$a = (5| | $b =7)
Correct result: true,7

Upgrade a bit
Variant one
Copy Code code as follows:

$a = 3;
$b = 6;
$c = 1;
if ($a =5| | $b =7 && $c =10) {
$a + +;
$b + +;
}
Var_dump ($a, $b, $c);

Variant two
Copy Code code as follows:

$a = 3;
$b = 6;
$c = 1;
if ($a =0| | $b =7 && $c =10) {
$a + +;
$b + +;
}
Var_dump ($a, $b, $c);

Interested students can think about:

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.