Jam*_*mol -5 php if-statement variable-assignment
请看这段代码:
function getVar(){
return "some string";
}
function test1(){
if($a = getVar()){
echo $a;
}
}
function test2(){
if($a = getVar() && $b = getVar()){
echo $a;
}
}
//test1();
test2();
Run Code Online (Sandbox Code Playgroud)
我想你会明白发生了什么.刚刚尝试test1()和test2()
为什么要test2()制作$a 1?
基本上,代码执行以下操作:if ($a = (getVar() && $b=getVar())).这没有错误."some string" && "some string" == true
您可以在doc中了解PHP中的运算符优先级.
| 归档时间: |
|
| 查看次数: |
110 次 |
| 最近记录: |