Precedence of Operations

This section provides the order of precedence for operations commonly used in PHP. Operations in a complex expression must be evaluated according to the order of operation precedence.

In the previous section, we learned that operations in a complex expression must be evaluated according to the order of operation precedence. The following table shows you the relative order of precedence for some commonly used operations:

Precedence   Operations       Notes
16           (...)            Operation group
15           ++ --            Increment/decrement
14           ~ -              Unary negation
13           !                Logical not
12           * / %            Multiplication, Division, ...
11           + -              Addition and Subtraction
10           .                String concatenation
9            << >>            Bitwise shift
8            < > <= >=        Comparisons
7            == != <> === !== Comparisons
6            &                Bitwise and
5            ^                Bitwise xor
4            |                Bitwise or
3            && And           Logical and
2            xor              Logical xor
1            || Or            Logical or
0            = += -= ...      Assignments

Remember that:

To show you some of reference rules mentioned above, I wrote the following PHP script, PrecedenceTest.php:

<?php
#  PrecedenceTest.php
#- Copyright 2003 (c) HerongYang.com. All Rights Reserved.
#
   print "\n Arithmetic, comparison and logical Operations:\n";
   print "    : ". (1 + 2 * 3 / 4) ."\n";
   print "    : ". (1 + 2 * 3 / 4 < 5 != FALSE) ."\n";
   print "    : ". (1 + 2 * 3 / 4 < 5 != FALSE || 6 < 7) ."\n";

   print "\n Operations with groups:\n";
   print "    : ". (((1 + (2 * 3 / 4) < 5) != FALSE) || 6 < 7) ."\n";
   print "    : ". ((1 + (2 * 3 / 4) < 5) != (FALSE || 6 < 7)) ."\n";

   print "\n Bitwise Operations:\n";
   print "    : " . (0x0001 | 0x00FF & 0x3210 << 4) . "\n";
   print "    : " . ((0x0001 | 0x00FF) & 0x3210 << 4) . "\n";
   print "    : " . (0x0001 | (0x00FF & 0x3210) << 4) . "\n";
   print "    : " . (0x0001 | 0x00FF & (0x3210 << 4)) . "\n";
?>

If you run this sample script, you should get:

herong> \php\php PrecedenceTest.php

 Arithmetic, comparison and logical Operations:
    : 2.5
    : 1
    : 1

 Operations with groups:
    : 1
    :

 Bitwise Operations:
    : 1
    : 0
    : 257
    : 1

Table of Contents

 About This Book

 Introduction and Installation of PHP

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

Expressions, Operations and Type Conversions

 What Is an Expression

 What Is an Operation

Precedence of Operations

 Data Type Automatic Conversion

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Interface with Operating System

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 Managing File Upload

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Parsing and Managing HTML Documents

 Configuring and Sending Out Emails

 Image and Picture Processing

 Managing ZIP Archive Files

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB