Can I write this if statement in PHP? I think it's more than that.

Source: Internet
Author: User
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.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.