PHP basic trap problem of variable assignment

  • 2020-05-24 05:16:17
  • OfStack

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


Trap 1

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

Trap 2

$a=5 was successfully assigned $b=7 was not executed
Error result: 6,7

A correct understanding

The trap is the precedence of the operator, and the assignment operator (=) has the lowest precedence, so the correct understanding is
$a=(5||$b=7)
Correct result: true,7

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

The deformation of 2
 
$a=3; 
$b=6; 
$c=1; 
if($a=0||$b=7 && $c=10){ 
$a++; 
$b++; 
} 
var_dump($a, $b,$c); 

Interested students can think 1 :)


Related articles: