PHP basic trap questions (variable assignment)_PHP tutorial

WBOY
Release: 2016-07-21 15:15:55
Original
904 people have browsed it

Copy code The code is as follows:

$a=3;
$b= 6;
if($a=5||$b=7){
$a++;
$b++;
}
var_dump($a, $b);


Trap 1

Considering $a=5, $b=7 as $a==5, $b==7
Error result: 3,6

Trap 2

Operator priority, it is considered that $a=5 is assigned successfully and $b=7 is not executed
Error result: 6,7

Correct understanding

The trap is the priority of operators. The assignment operator (=) has the lowest priority, so the correct understanding should be
$a=(5||$b=7)
Correct Result: true,7

Upgrade
Transformation 1
Copy code The code is as follows:

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

Variation 2
Copy code The code is 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 it:)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326013.htmlTechArticleCopy the code as follows: ?php $a=3; $b=6; if($a=5| |$b=7){ $a++; $b++; } var_dump($a, $b); Trap: $a=5, $b=7 is regarded as $a==5, $b==7, error Result: 3,6 Advantages of the trap binary operator...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!