- PHP Operators
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Incrementing/ Decrementing Operators
- Concatenating Operator
- Assignment Operators
- PHP operators precedence
- PHP Operators
- PHP Arithmetic Operators
- PHP Assignment Operators
- PHP Comparison Operators
- PHP Increment / Decrement Operators
- PHP Logical Operators
- PHP String Operators
- PHP Array Operators
- PHP Conditional Assignment Operators
PHP Operators
Summary: in this tutorial, you will learn about PHP operators and how to use them effectively in your script.
An operator takes one or more values, known as operands, and performs a specific operation on them.
For example, the + operator adds two numbers and returns the sum of them.
PHP supports many kinds of operators:
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Increment/Decrement Operators
- Logical Operators
- Concatenating Operators
Arithmetic Operators
The arithmetic operators require numeric values. If you apply them to non-numeric values, they’ll convert them to numeric values before carrying the arithmetic operation.
The following are the list of arithmetic operators:
Operator | Name | Description |
---|---|---|
+ | Addition | Return the sum of two operands |
– | Subtraction | Return the difference between two operands |
* | Multiplication | Return the product of two operands |
/ | Division | Return the quotient of two operands |
% | Modulus | Return the remainder of the division of the first operand by the second one |
The following example uses the arithmetic operators:
$x = 20; $y = 10; // add, subtract, and multiplication operators demo echo $x + $y . '
'; // 30 echo $x - $y . '
'; // 10 echo $x * $y . '
'; // 200 // division operator demo $z = $x / $y; echo gettype($z) . '
'; // integer $z = $y / $x; echo gettype($z) . '
'; // double // modulus demo $y = 15; echo $x % $y . '
'; // 5
Code language: HTML, XML (xml)
Comparison Operators
Comparison operators allow you to compare two operands.
A comparison operator returns a Boolean value, either true or false . If the comparison is truthful, the comparison operator returns true , otherwise, it returns false .
The following are the list of comparison operators in PHP:
Operator | Name | Description |
---|---|---|
== | Equality | Return true if both operands are equal, otherwise returns false . |
=== | Identity | Return true if both operands have the same data type and equal, otherwise return false . |
!=== | Not identical | Return true if both operands are not equal or not have the same data type, otherwise return false . |
> | Greater than | Return true if the operand on the left is greater than the operand on the right, otherwise return false . |
>= | Greater than or equal to | Return true if the operand on the left is greater than or equal to the operand on the right, otherwise return false . |
Less than | Return true if the operand on the left is less than the operand on the right, otherwise return false . | |
Less than or equal | Return true if the operand on the left is less than or equal to the operand on the right, otherwise return false . |
Logical Operators
Logical operators allow you to construct logical expressions. A logical operator returns a Boolean value.
PHP provides the following logical operators:
Operator | Name | Description |
---|---|---|
&& | Logical AND | Return true if both operands are true , otherwise return false . If the first operand is false , it will not evaluate the second operand because it knows for sure that the result is going to be false . This is known as short-circuiting. |
|| | Logical OR | Return true if one of the operands is true , otherwise returns false . If the first operand is true , it will not evaluate the second one. |
xor | Logical XOR | Return true if either operand, not both, is true , otherwise, return false . |
! | Not | returns true if the operand is false , and returns false if the operand is true . |
Bitwise Operators
Bitwise operators perform operations on the binary representation of the operands. The following illustrates bitwise operators in PHP:
Operators | Name | Result |
---|---|---|
$x & $y | And | If both bits are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0 |
$x | $y | Or (inclusive or) | If both bits are 0, the corresponding bit in the result is 0; otherwise, the corresponding bit is 1 |
$x ^ $y | Xor (exclusive or) | If either bit, but not both, in $x and $y are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0 |
~ $x | Not | Change bit 1 to 0 and 0 to 1 in the $x operand |
$x | Shift left | Shifts the bits in $x left by the number of places specified by $y . |
$x >> $y | Shift right | Shifts the bits in $x right by the number of places specified by $y . |
Incrementing/ Decrementing Operators
Increment (++) and decrement (–) operators give you a quick way to increase and decrease the value of a variable by 1.
The following table illustrates the increment and decrement operators:
Example | Name | Returned Value | Effect on $a |
---|---|---|---|
++$a | Pre-increment | $a + 1 | Increments $a by 1, then returns $a . |
$a++ | Post-increment | $a | Returns $a , then increments $a by 1. |
—$a | Pre-decrement | $a — 1 | Decrements $a by 1, then returns $a . |
$a— | Post-decrement | $a | Returns $a , then decrements $a by 1. |
Concatenating Operator
Concatenating operator (.) allows you to combine two strings into one. It appends the second string to the first one and returns the combined string. For example:
$str = 'PHP' . ' is ' . ' Awesome!'; echo $str;
Code language: HTML, XML (xml)
Assignment Operators
Assignment operator ( = ) assigns a value to a variable and returns a value. The operand on the left is always a variable, while the operand on the right can be a literal value, variable, expression, or a function call that returns a value. For example:
$x = 10; $y = $x; $z = ($x = 20); // $z = 20
Code language: HTML, XML (xml)
In the first expression, we assigned $x variable value 10 . In the second one, we assigned the value of $x to $y variable. The third one is a little bit complicated. First, we assigned 20 to $x . The assignment operator ( = ) returns 20 and then 20 is assigned to $z variable.
Besides the basic assignment operator( = ), PHP provides you with some assignment operators:
- plus-equal +=
- minus-equal -=
- divide-equal /=
- multiplication-equal *=
- modulus-equal %=
- XOR-equal ^=
- AND-equal &=
- OR-equal |=
- concatenate-equal .=
PHP operators precedence
The precedence of an operator decides which order the operator is evaluated in an expression.
PHP assigned each operator precedence. Some operators have the same precedence, e.g., precedences of the addition ( + ) and subtraction( — ) are equal.
However, some operators have higher precedence than others.
For example, the precedence of the multiplication operator ( * ) is higher than the precedence of the add( + ) and the subtract ( — ) operators:
echo 4 + 5 * 3; // 19
Code language: HTML, XML (xml)
Because the precedence of the multiplication operator ( * ) is higher than the precedence of the add( + ) operator, PHP evaluates the multiplication operator ( * ) first and then add operator ( * ) second.
To force the evaluation in a particular order, you put the expression inside parentheses () , for example:
echo (4 + 5) * 3; // 27
Code language: HTML, XML (xml)
In this tutorial, you have briefly learned about the most commonly used PHP operators.
PHP Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
PHP Arithmetic Operators
The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
Operator | Name | Example | Result | Show it |
---|---|---|---|---|
+ | Addition | $x + $y | Sum of $x and $y | Try it » |
— | Subtraction | $x — $y | Difference of $x and $y | Try it » |
* | Multiplication | $x * $y | Product of $x and $y | Try it » |
/ | Division | $x / $y | Quotient of $x and $y | Try it » |
% | Modulus | $x % $y | Remainder of $x divided by $y | Try it » |
** | Exponentiation | $x ** $y | Result of raising $x to the $y’th power | Try it » |
PHP Assignment Operators
The PHP assignment operators are used with numeric values to write a value to a variable.
The basic assignment operator in PHP is «=». It means that the left operand gets set to the value of the assignment expression on the right.
Assignment | Same as. | Description | Show it |
---|---|---|---|
x = y | x = y | The left operand gets set to the value of the expression on the right | Try it » |
x += y | x = x + y | Addition | Try it » |
x -= y | x = x — y | Subtraction | Try it » |
x *= y | x = x * y | Multiplication | Try it » |
x /= y | x = x / y | Division | Try it » |
x %= y | x = x % y | Modulus | Try it » |
PHP Comparison Operators
The PHP comparison operators are used to compare two values (number or string):
Operator | Name | Example | Result | Show it |
---|---|---|---|---|
== | Equal | $x == $y | Returns true if $x is equal to $y | Try it » |
=== | Identical | $x === $y | Returns true if $x is equal to $y, and they are of the same type | Try it » |
!= | Not equal | $x != $y | Returns true if $x is not equal to $y | Try it » |
<> | Not equal | $x <> $y | Returns true if $x is not equal to $y | Try it » |
!== | Not identical | $x !== $y | Returns true if $x is not equal to $y, or they are not of the same type | Try it » |
> | Greater than | $x > $y | Returns true if $x is greater than $y | Try it » |
Less than | $x < $y | Returns true if $x is less than $y | Try it » | |
>= | Greater than or equal to | $x >= $y | Returns true if $x is greater than or equal to $y | Try it » |
Less than or equal to | $x | Returns true if $x is less than or equal to $y | Try it » | |
Spaceship | $x $y | Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7. | Try it » |
PHP Increment / Decrement Operators
The PHP increment operators are used to increment a variable’s value.
The PHP decrement operators are used to decrement a variable’s value.
Operator | Name | Description | Show it |
---|---|---|---|
++$x | Pre-increment | Increments $x by one, then returns $x | Try it » |
$x++ | Post-increment | Returns $x, then increments $x by one | Try it » |
—$x | Pre-decrement | Decrements $x by one, then returns $x | Try it » |
$x— | Post-decrement | Returns $x, then decrements $x by one | Try it » |
PHP Logical Operators
The PHP logical operators are used to combine conditional statements.
Operator | Name | Example | Result | Show it |
---|---|---|---|---|
and | And | $x and $y | True if both $x and $y are true | Try it » |
or | Or | $x or $y | True if either $x or $y is true | Try it » |
xor | Xor | $x xor $y | True if either $x or $y is true, but not both | Try it » |
&& | And | $x && $y | True if both $x and $y are true | Try it » |
|| | Or | $x || $y | True if either $x or $y is true | Try it » |
! | Not | !$x | True if $x is not true | Try it » |
PHP String Operators
PHP has two operators that are specially designed for strings.
Operator | Name | Example | Result | Show it |
---|---|---|---|---|
. | Concatenation | $txt1 . $txt2 | Concatenation of $txt1 and $txt2 | Try it » |
.= | Concatenation assignment | $txt1 .= $txt2 | Appends $txt2 to $txt1 | Try it » |
PHP Array Operators
The PHP array operators are used to compare arrays.
Operator | Name | Example | Result | Show it |
---|---|---|---|---|
+ | Union | $x + $y | Union of $x and $y | Try it » |
== | Equality | $x == $y | Returns true if $x and $y have the same key/value pairs | Try it » |
=== | Identity | $x === $y | Returns true if $x and $y have the same key/value pairs in the same order and of the same types | Try it » |
!= | Inequality | $x != $y | Returns true if $x is not equal to $y | Try it » |
<> | Inequality | $x <> $y | Returns true if $x is not equal to $y | Try it » |
!== | Non-identity | $x !== $y | Returns true if $x is not identical to $y | Try it » |
PHP Conditional Assignment Operators
The PHP conditional assignment operators are used to set a value depending on conditions:
Operator | Name | Example | Result | Show it |
---|---|---|---|---|
?: | Ternary | $x = expr1 ? expr2 : expr3 | Returns the value of $x. The value of $x is expr2 if expr1 = TRUE. The value of $x is expr3 if expr1 = FALSE | Try it » |
?? | Null coalescing | $x = expr1 ?? expr2 | Returns the value of $x. The value of $x is expr1 if expr1 exists, and is not NULL. If expr1 does not exist, or is NULL, the value of $x is expr2. Introduced in PHP 7 | Try it » |