Operator "and" for assignment in classes

7

Looking at some repositories of GitHub I found this repository for generation of tickets with php and within the class Cedente caught my attention the use of the word and within the function construct in this file .

The and operator is used for comparisons and can also be written as && as if in the example below;

if (x = 10 and y = 20) { ...

In this file, what is the function of and ?

$endereco and $this->setEndereco($endereco);
    
asked by anonymous 05.05.2017 / 19:02

1 answer

7

The same as in if , after all this is a Boolean operator, it can be applied in any expression , it does not have to be in if as many people think. I do not understand why they think + can be used anywhere and & or and can not.

Since and uses the short circuit technique the second operand will only be executed if the first true. So if the variable of the first operand is null, it is false (awful thing, but so is PHP), then the assignment will not be executed. If it has some value then it is true, then the assignment of the second operand will be executed.

I get the impression that its usage there is unnecessary, because if it did not have this mechanism it would assign null to something that should be null anyway. Maybe it's an optimization attempt, but if optimization is needed, it should do in another language.

See more .

    
05.05.2017 / 19:11