Home  >  Article  >  php教程  >  php中一个"异类"语法: $a && $b = $c;

php中一个"异类"语法: $a && $b = $c;

WBOY
WBOYOriginal
2016-07-09 09:08:051252browse

php中一个"异类"语法: $a && $b = $c;

   
$a = 1;
$b = 2;
$c = 3;
$a && $b = $c;
echo "a:$a";
echo "b:$b";
echo "c:$c";
这样是
a:1
b:3
c:3
$a = 0;
$b = 2;
$c = 3;
$a && $b = $c;
echo "a:$a";
echo "b:$b";
echo "c:$c";
这样是
a:0
b:2
c:3
解析:
1:  && 和赋值运算符“=”的优先级是&&优先级高,但是加红的写法中并不是 $a和$b先做与运算然后 =$c,这么理解相当于 1=$c, 而 1=$c这样的写法是错误的
2: 这种另类的写法正确的理解方式为:
if($a){$b = $c},即如果$a为真,则执行赋值语句$b = $c;否则不执行此赋值语句,这样结果不难理解
Statement:
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