difficulty to understand a PHP syntax

Viewed 55

Suppose we have this method in a class :

public static function example($s, $f = false)
{
    (static::$i['s'] === null or $f) and static::$i['s'] = $s;
    return new static;
}

would you please give me any hint what is the meaning of this line of code?

(static::$i['s'] === null or $f) and static::$i['s'] = $s;

is it a conditional statement? or something like a ternary operator? Thanks

1 Answers

They are trying to be clever by using the short circuiting that happens when dealing with logical operators like this. This is what they are doing:

if ((static::$info['syntax'] === null) || $force) {
    static::$info['syntax'] = $syntax;
}

If you want to see how this short circuiting works with the &&/and operator:

$a = 'something';
false && $a = 'else';
echo $a;
// output: something

Since the first part is false, it never even runs the statement on the other side since this logical operation already evaluates to false.

Related