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 array_key_first Function: How it Works with Examples

    The array_key_first helps you to get the first key of an array directly in PHP. It saves time and removes…

    PHP Type Casting: How to Convert Variables in PHP

    Type casting in PHP refers to the process of converting a variable from one data type to another. This is…

    PHP error_log Function: How It Works with Examples

    Sometimes things go wrong when PHP runs the code. So, you need to know the reasons for this error to…

    PHP is_dir Function: Check if a Directory Exists

    Sometimes, when you run a script, you may encounter an error because the directory you're trying to access doesn't exist.…

    Understanding the PHP Enumerable

    Actually, the PHP enumerable is a new feature of 8.1 which allows you to define a new type. This feature…

    PHP array_fill Function: Fill Array with Same Value

    You can use the array_fill function in PHP when you must fill an array with one value. You may want…

    PHP htmlspecialchars Function: Prevent XSS in HTML Output

    A PHP script can break the page or allow code injection if it outputs user input directly into HTML. The…

    PHP strpos Function: How it Works with Examples

    Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result…

    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…

    How to Insert Multiple Rows in MySQL with PHP?

    Inserting multiple data rows using PHP is a basic task. However when you start working with MySQL databases, So that…

    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.