Understanding the PHP Operator Precedence

The PHP operator precedence refers to when doing a calculation for three or more numbers, they are calculating its values according to the priority. For example, 10 + 10 × 2, the result is 30 and not 40. That will lead us to explain the reasons in the following sections.

    The PHP operator precedence, like in the below down table.

    OperatorSmall Description
    ** – / %Arithmetic
    ++ —Increment and Decrement
    ( ! – || – && – or – xor – and )The Logical Operators
    */Multiplication and Division
    %modulo
    + – .Arithmetic and String
    ??Null Coalescing
    =Assignment Operators
    ( | – ^ – & – << >> )Bitwise
    &References
    == != === !== <> <=>Comparison Operator
    (flat)(int)(string)(bool)Casting
    new clonenew and clone objects
    yield fromyield from
    yieldyield
    printprint

    The following section shows you examples for the PHP operator precedence.

    Arithmetic Examples

    <?php 
      $calcs = 10 + 20 * 5;
      echo $calcs; // 110
    ?>

    In the previous example, the highest precedence was for the multiplication, So it was written behind the scenes like this: (20 × 5) = 100 and add 10, so it would 110.

    Also, check the multiplication with the modulo operator. You will see the same highest precedence for multiplication.

    <?php 
      $calcs = 20 * 3 % 50;
      echo $calcs; // 10
    ?>

    So, it has written as the 20 * 3 = 60. And then the modulo operator is added to the result as the 60 % 50. So the result was 10.

    Anyway, in the following example, you will see another kind of PHP operator precedence with the PHP variable assignment.

    Use PHP Operator Precedence with Assignment

    <?php 
      $x = 10;
      $f = &$x;
      $y = 20;
      $x = $y += 5;
    
      echo $f;
      echo "\n";
      echo $y;
      echo "\n";
      echo $x;
    ?>

    The result of this example would be like the following.

    25
    25
    25

    The three variables produced the same value. Let’s demonstrate each one.

    The first printed variable echo $f was 25 and not 10. That is because it has a reference operator, it will remain for the last value for the &$x variable. So the last value for the $x was 25.

    On the other hand, the $y is produced 25 also because we added +5 in the last variable $x = $y += 5;. So the old one was 20 and added +5 to be 25.

    And in the final one, it produced a result of 25 because it assigned the $y variable and the $y variable also produced 25 for the previous reason.

    Anyway, PHP operator precedence also can be returning undefined values with assigned arithmetic. So the potential result for the calculation is two results. Which are called undefined order of evaluation. Let’s see an example.

    <?php 
      $x = 10;
      echo $x + $x++; // it may print 21 or 20
    ?>

    Logical Priority

    In the next example, there are two values for a boolean data type. The priority would be the positive one. Let’s take an example.

    <?php 
      var_dump( true || false ); // bool(true)
    ?>

    Also, if it is with AND operator, it will show you false as a boolean result.

    <?php 
      var_dump( true && false ); // bool(false )
    ?>

    Thank you for reading.

    Similar Reads

    PHP File Inclusion: require, include, require_once, include_once

    PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…

    PHP array_intersect_key Function: How it Works with Examples

    The array_intersect_key in PHP compares arrays by keys and returns only the parts that match. It is useful when you…

    How to Connect PHP to MongoDB

    Connecting PHP to MongoDB allows you to integrate database functionalities into your web applications. MongoDB is a NoSQL database that…

    PHP Assign Function to Variable – Examples and Explanation

    PHP variable function is a variable that is used to assign a function name with parentheses (). However, the PHP interpreter…

    PHP $_ENV: Manage Environment Variables

    In this tutorial, we will explore a form of superglobal variable in PHP: $_ENV, which symbolizes how PHP reaches for…

    Abstract Class in PHP: How It Works & Examples

    Abstract class in PHP appeared to provide a way to define a structure for related classes and don't allow direct…

    PHP is_file Function: Check if a File Exists

    If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will…

    PHP function_exists: Avoid Function Redeclaration

    The function_exists() function in PHP checks whether a given function is defined or not. The function_exists() function works for both…

    PHP substr Function: How to Extract and Manipulate Strings

    The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…

    PHP fread Function: How to Read Files in PHP

    In some cases, you need to handle a lot of data or simply try to open a file, or read…

    Previous Article

    PHP Switch | How the Switch Statement Works in PHP

    Next Article

    PHP Arithmetic Operators: Essential Guide

    Write a Comment

    Leave a Comment

    Your email address will not be published. Required fields are marked *


    Subscribe to Get Updates

    Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
    No spam. Unsubscribe anytime.