Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array assigned with and

Tags:

php

Just learning php and looking into someone else's code. I'm not sure what is happening in this function with the word 'and' on the left side of the = operator. It seems like it is a 'silent' if is being used eg. if $arry =true and $array2 = true then $array2 += 'somthing';

I cant seem to find any reference to this anywhere online.

  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;
  }
like image 646
Elijha Avatar asked Mar 06 '26 16:03

Elijha


2 Answers

$foo and $bar = "baz";

Is just a confusing way of saying

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

Shame on whoever wrote that.

like image 80
Matti Virkkunen Avatar answered Mar 08 '26 08:03

Matti Virkkunen


It means that if the left side of the and evaluates to true, the right side will be executed also. In essence, if clientID evaluates to true (is non-false), then sprintf("(WD_domain.clientID = %d)",$filter["clientID"]) will be added to the $sql array.

It's a lazy way of doing this:

if($filter["clientID"]) {
    $sql[] = sprintf("(WD_domain.clientID = %d)",$filter["clientID"]);
}
like image 26
Tatu Ulmanen Avatar answered Mar 08 '26 08:03

Tatu Ulmanen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!