为什么结果是下面结果,请大牛帮忙解释一下

WBOY
Release: 2016-06-23 14:09:40
Original
709 people have browsed it

<?php$a = 4;$b = 5;if($a=6 && $b=7){	$a++;	$b++;}echo var_dump($a)."_".var_dump($b);
Copy after login

为什么结果是下面结果,请大牛帮忙解释一下
bool(true)
int(8)


回复讨论(解决方案)

$a被赋值成了6&& $b=7
原式相当于$a = (6 && $b=7)
因为(6 && $b=7)为true,所以返回了bool(true)
因为条件是true,所以$b++生效,$b最后为8

$a = 4;$b = 5;if($a=6 && $b=7){ //由于 && 的优先级高于 = 所以是 $a = (6 && $b=7)    var_dump($a, $b); //bool(true) int(7)    $a++; //逻辑值没有算数运算,$a 不会改变    $b++; //$b 加一}echo var_dump($a, $b); //bool(true) int(8)
Copy after login

写成这样,应该能帮助你理解(and 的优先级低于 = )
$a = 4;$b = 5;if($a=6 and $b=7){    var_dump($a, $b);//int(6) int(7)    $a++;    $b++;}echo var_dump($a, $b);//int(7) int(8)
Copy after login

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