Operator "and" for class assignment

Asked

Viewed 51 times

7

Looking at some Github repositories I found this repository for generation of boletos with php and within the class Cedente I was struck by the use of the word and within the function construct in this file.

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

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

In this file shown what the function of the and?

$endereco and $this->setEndereco($endereco);

1 answer

7


The same as in if, after all this is a boolean operator, it can be applied in any expression that requires a true or false result, it does not need to be in a if as many people think. I do not understand why they think that the + can be used anywhere and the & or and can’t.

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

I have the impression that its use there is unnecessary, because if it did not have this mechanism would assign null to something that should be null even. It may be an attempt at optimization, but if optimization is necessary, it should be done in another language.

See more.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.