Php array assigned and

Just by studying php and looking at another user's code. I'm not sure what happens in this function with the word "and" on the left side of the = operator. It seems to be "silent" if used, for example. if $ arry = true and $ array2 = true, then $ array2 + = 'somthing';

I cannot find a link to this anywhere on the Internet.

  function get_list_filter($filter = array()) {
     global $current_user;
    $sql = array();
    $filter["clientID"]         and $sql[] = sprintf("(WD_domain.clientID = %d)",$filter["clientID"]);
    $filter["showDomainName"]   and $sql[] = sprintf("(WD_domain.domain LIKE '%%%s%%')",$filter["showDomainName"]);
    $filter["showManaged"]      and $sql[] = sprintf("(WD_domain.managed = %d)",$filter["showManaged"]);
    return $sql;
  }
+3
source share
2 answers

This means that if the left side andis true, the right side will also be executed. In essence, if clientIDevaluates to true (not false), it sprintf("(WD_domain.clientID = %d)",$filter["clientID"])will be added to the array $sql.

This is the lazy way to do this:

if($filter["clientID"]) {
    $sql[] = sprintf("(WD_domain.clientID = %d)",$filter["clientID"]);
}
+4
source
$foo and $bar = "baz";

It's just a confusing way to say

if ($foo)
    $bar = "baz";

Shame on the one who wrote it.

+5
source

Source: https://habr.com/ru/post/1790277/


All Articles