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.
This operator can be categorized into two types. These are,
<?php
echo ++i; // increment before print
echo --i; // decrement before print
?>
<?php
i++; // increment after print
i--; // decrement after print
?>
Addition (+), Subtraction (-), Division (/), Multiplication (*) and modulus (%) operators are coming under this category. Among them, (+,-) has lower precedence than rest of the arithmetic 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.
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 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;
?>
Apart from the above list of operators, PHP includes some additional list of operators. These are,
<?php
$user = "PHP Guest";
echo "Hello" . $user . "<br/>";
?>
<?php
$output = $_SESSION["userName"] ? $_SESSION["userName"] : "ANONYMOUS";
echo $output;
?>
<?php
$output = `ls -al`;
?>
<?php
@split("-",date("d-m-Y");
?>
By using @ symbol in front of split function which is deprecated, we can suppress the PHP error message.