PHP Operators

by Vincy. Last modified on July 2nd, 2022.

Like other programming languages, PHP includes set of operators that are used to perform operations between one or more values.

PHP has a variety of operators based on the operations to be performed. Each operator has precedence, depends on which order a PHP expression will be executed. Subexpressions that hold operators with higher precedence are having higher priority during execution.

In this article, we are going to see about various type of PHP operators in the order of their precedence.

Auto increment/decrement operators in PHP

This operator can be categorized into two types. These are,

    1. pre-increment/decrement. For example,
      <?php
      echo ++i; // increment before print
      echo --i; // decrement before print
      ?>
      
    2. post-increment/decrement. For example,
      <?php
      i++; // increment after print
      i--; // decrement after print
      ?>
      

Arithmetic Operators

Addition (+), Subtraction (-), Division (/), Multiplication (*) and modulus (%) operators are coming under this category. Among them, (+,-) has lower precedence than rest of the arithmetic operators.

PHP Comparison Operators

Comparison operators are used in conditional expressions of PHP control flow statements like, if, else if, while and so on. For example,

<?php
while (i <= 10) {
    
}
?>

In PHP, the following comparison operators are available.

<, >, <=, >=, ==, === and !=

Among them, the first four operators are having higher precedence than others.

Logical Operators

PHP includes the following list of logical operators.

&&, ||, and, or, xor

Logical operators represented by symbols and words, i.e. (or and ||) return the same result, but vary with the precedence. The symbol representation has two level higher precedence than the word representation of logical operators. Because assignment and combined assignment operators are having higher precedence than the word representation of logical operators.

Assignment / combined assignment operators in PHP

Assignment operators are used either to create a copy of PHP variable or to store some value in the new variable.

Combined assignment operators are used to reduce the length of the expression and number of operands. For example,

<?php
$i = $i + 1;
// using combined arithmetic operator
$i += 1;
?>

Other additional PHP Operators

Apart from the above list of operators, PHP includes some additional list of operators. These are,

  • Concatenation operator – dot (.) is used to concatenate two or more strings, or to add HTML code into PHP print statements. For example,
    <?php
    $user = "PHP Guest";
    echo "Hello" . $user . "<br/>";
    ?>
    
  • Conditional operator – ( <condition> ? <if true> : <if false> ). For example,
    <?php
    $output = $_SESSION["userName"] ? $_SESSION["userName"] : "ANONYMOUS";
    echo $output;
    ?>
    
  • Backticks – PHP supports to execute a shell command, by enclosing it within two backticks (“) in a PHP printing statements. For example,
    <?php
    $output = `ls -al`;
    ?>
    
  • Suppression operator – (@) is used to suppress any warning or error notice from displaying to the browser. For example,
    <?php
    @split("-",date("d-m-Y");
    ?>
    

    By using @ symbol in front of split function which is deprecated, we can suppress the PHP error message.

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

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

↑ Back to Top

Share this page