Dino Cajic on PHP Operator Precedence

Which Operators Come First?

PHP Operator precedence just looks at which operation will be performed first. You’ve seen operator precedence in mathematics. The multiplication and division operators are executed before the addition and subtraction operators. With all of the different operators that we have in PHP, the interpreter needs to know which operation to perform in what order.

Let’s look at a basic example in math to get started:

2 + 3 * 5

If you were not aware of operator precedence in mathematics, you may start working from left to right. The answer that you would get would be incorrect: 25. However, we all took math and we know that multiplication comes before addition, so we get 17.

You don’t have to know the precedence of each operator: you can always look it up if you encounter something strange. However, the ones you should be familiar with are the following:

  • new
  • ** (exponential)
  • ++ — (increment/decrement)
  • ! (logical not)
  • * / % (arithmetic)
  • + – . (arithmetic/concatenation)
  • == != === !== <> <=> (comparison)
  • && (logical)
  • || (logical)
  • ?: (ternary)
  • = += -= *= /= %= (assignment)
  • and (logical)
  • xor (logical)
  • or (logical)

Why does operator precedence matter so much? You already know which operations will be separate in mathematics, so what’s the big deal? The time that it might cause you the most amount of issue is during logical operations.

Let’s take a look at the following example:

 

<?php var_dump( true || !true && false );?>

 

If we worked from left to right, we might evaluate the expression as follows

  1. true || !true
  2. true || false = true
  3. true && false = false

The result that you get is false. In order for the OR (||) operator to be true, either the left or the right side has to be true (or both). Since the left side is true, the overall expression is true. For the AND (&&) operator to return true, both sides have to be true. Since the right side is false, the overall expression is false.

Of course we received the wrong result. Let’s take a look at the result when we factor in operator precedence. The operators in the expression above are !, &&, and ||. The ! operator has the highest precedence, followed by &&, and finally finishing up with the || operator. So let’s see what that looks like.

  1. !true && false
  2. false && false = false
  3. true || false = true

This time we get true. Following operator precedence is important. If we wanted to achieve the result that we achieved in the first example, we would have to use parentheses.

 

<?php var_dump( (true || !true) && false );?>

 

When we use parentheses, we’re telling PHP to do that operation first. This time we get false.

One thing to note is the && operator. The way the && operator works is that it looks at the expression on the left, and if it’s false, it won’t even look at the expression on the right. PHP programmers don’t do this too often, but I’ve seen programmers in other programming languages use this too their advantage, and sometimes to their disadvantage. If you’ve done any type of programming using JavaScript’s React library, you’ll quickly notice that programmers like to take advantage of this fact. If the expression on the left evaluates to true, execute some content on the right. This is called conditional rendering in React.

To get the full list of operators and their precedence, check out the list on PHP.

 

 

PHP Tutorial Series

Continue your learning with these articles

PHP Shorthand Operators

My Hands Aren’t Short!

PHP – P22: Shorthand Operators

PHP shorthand operators are amazing! There, I said it. The shorthand operator combines the expression on the right with the assignment operator.

Dino Cajic describing PHP Operand Precedence

Which Operators Come First?

PHP – P23: Operator Precedence

PHP Operator precedence just looks at which operation will be performed first. You’ve seen operator precedence in mathematics. The multiplication and division operators are executed before the addition and subtraction operators. With all of the different operators that we have in PHP, the interpreter needs to know which operation to perform in what order.

Dino Cajic on PHP IF Statements

IF YOU ONLY KNEW

PHP – P24: IF STATEMENT

The if statement is probably the first piece of code that you wrote. “If” statements are conditional statements, meaning that if the expression that they’re evaluating is true, the if statement will allow for the execution of the expressions inside of the if statement body.

Leave a Reply