Can I write this if statement in PHP? I think it's a lot more.
Public Function getarealist ($pid = 1) {
$map = Array ();
$pid! =-1 && $map [' pid '] = $pid;
return $this->where ($map)->order (' area_id ' ASC ')->findall ();
}
$pid! =-1 && $map [' pid '] = $pid;
return $this->where ($map)->order (' area_id ' ASC ')->findall ();
There's no relationship between the two sentences, right? Why do I use && to connect the above sentence? With direct write
$pid! =-1;
$map [' pid '] = $pid;
What is the difference?
Share to:
------Solution--------------------
&& is the logic and operation, and the assertion means that the previous expression bit is true before the following one is executed. If False, the second expression will not execute
------Solution--------------------
$pid! =-1 && $map [' pid '] = $pid;
Equivalent
if ($pid! =-1) {
$map [' pid '] = $pid;
}
This is because && is a short-circuit operation, and if the previous expression in && is not true, the subsequent expression is not executed.
In fact, it is not recommended to write, one is not clear, easy to confuse, and second, if the && later content changes are prone to error.