What does && mean! in PHP?

3

I'm trying to know this, but it appears nowhere. Has a space between & e!.

if ( is_home() && ! is_front_page() )
    
asked by anonymous 24.06.2017 / 18:44

2 answers

12
Spaces generally do not make a difference to the code. At least in PHP almost always not. Which is even a pity because programmers make atrocities and make the code less readable, inconsistent. Kind of like they do with natural language. Particularly I would write like this:

if (is_home() && !is_front_page())

It's more obvious associativity. Too much to overdo it can get worse.

The ! ( not ) is denying the result of is_front_page() which should be a Boolean or a data that can be represented as a Boolean, such as 0 or another value.

How it is unary will be associated with the one on the right and it has a precedence .

The && ( and ) operator is binary and therefore it will apply in the expressions to the left and right of it. It has a precedence lower than not , then it will be executed later, if the precedence was greater, to get the same result, it would have to do:

is_home() && (!is_front_page()))

Space does not change precedence, and I only knew a language that did this and it was very confusing.

Then this whole expression needs that the first method returns a true one and the second one returns a false, since it will be inverted, then the entire condition of if will be true and will execute its code block.

In the precedence linked question above you have more information about and . You have about not in JavaScript that works the same.

Obviously, if you write &&! would be another operator, you do not have the compiler to know that they are two, and since this operator does not exist, it would give error. This is a point where space is fundamental.

    
24.06.2017 / 19:03
1

That:

if (is_home() && ! is_front_page())

It's the same as this:

if (is_home() && !(is_front_page()))

That is, because of ! , the condition will only be true if is_home() returns true and is_front_page() returns false.

    
29.06.2017 / 15:15