operators || or OR in PHP

1

What is the best and how to use it?

I'm trying to run this code for example and it does not work

if(1 !== 1 || 2) {
    echo true;
}

It displays true being that it was only to show if X (in case 1) was indifferent to 1 or 2.

    
asked by anonymous 23.07.2015 / 01:04

1 answer

4

The if can be translated as se 1 não é identico a 1 OU dois(que é true) , while changing the values of the logical values, SE(falso OU verdadeira) ENTAO ... .

The first part of the sentence ( 1!==1 ) is false and the second (only 2 ) is considered true for php, since the language works with automatic casts, it defines a series of values that can be evaluated as false and anything outside it is true , as the sentences are connected through || (logical OR) a true value to validate the condtion is sufficient.

The difference between || and OR is that OR has less priority or in this case be wise for unexpected results or to add parentheses in the condition.

Recommended reading

List of false values for PHP

What's the difference between "& &" and "||" and "and" and "or" in PHP ? Which one to use?

    
23.07.2015 / 01:14