Summary: in this tutorial, you will learn how to use PHP operators so that you can use them to manipulate data in your script.
Table of Contents
An operator takes one or more values, which are known as operands, and performs operation on them such as adding them together.
PHP borrows most of its operators from Perl and C. The following are the most common used operators in PHP:
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Comparison Operators
- Increment/Decrement Operators
- Logical Operators
- Concatenating Operators
Arithmetic Operators
The arithmetic operators require numeric values. And non-numeric values are converted automatically to numeric values. The following are the list of arithmetic operators:
- Addition (
+
) returns the sum of two operands - Subtraction (
-
) returns the difference between two operands - Multiplication (
*
) return the product of two operands - Division (
/
) returns the quotient of two operands. The result of division of two integers may produce an integer (4/2 = 2
) or a floating-point number (3/2 = 1.5
) - Modulus (
%
) returns the remainder of the division of the first operand by the second operand. Both operands are integers.
The following example demonstrates PHP arithmetic operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php $x = 20; $y = 10; // add, subtract, and multiplication operators demo echo $x + $y . '<br/>'; // 30 echo $x - $y . '<br/>'; // 10 echo $x * $y . '<br/>'; // 200 // division operator demo $z = $x / $y; echo gettype($z) . '<br/>'; // integer $z = $y / $x; echo gettype($z) . '<br/>'; // double // modulus demo $y = 15; echo $x % $y . '<br/>'; // 5 |
Comparison Operators
PHP provides comparison operators 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:
- Equality (
==
) returnstrue
if both operands are equal, otherwise returnsfalse
. - Identity(
===
) returntrue
if both operands have the same data type and equal, otherwise returnfalse
. - Inequality(
!=
or<>
) returnstrue
if both operands are not equal, otherwise returnfalse
. - Not identical (
!===
) returnstrue
if both operands are not equal or not have the same data type, otherwise returnsfalse
. - Greater than (
>
) returnstrue
if the operand on the left is greater than operand on the right, otherwise returnsfalse
. - Greater than or equal (
>=
) returnstrue
if the operand on the left is greater than or equal to operand on the right, otherwise returnsfalse
. - Less than (
<
) returnstrue
if the operand on the left is less than operand on the right, otherwise returnsfalse
. - Less than or equal (
<=
) returnstrue
if the operand on the left is less than or equal to operand on the right, otherwise returnsfalse
.
Logical Operators
Logical operators help you build a logical expression. A logical operator treats its operands as Boolean values and returns a Boolean value. PHP provides the following logical operators:
- Logical AND (
&&
,and
) returnstrue
if both operands aretrue
, otherwise returnsfalse
. If the first operand isfalse
, it will not evaluate the second operand because it knows for sure that the result is going to befalse
. This is known as short-circuiting. - Logical OR (
||
,or
) returntrue
if one of the operands istrue
, otherwise returnsfalse
. If the first operand istrue
, it will not evaluate the second one. - Logical XOR (
xor
) returnstrue
if either operand, not both, istrue
, otherwise returnsfalse
. - Logical negation (
!
) returnstrue
if the operand isfalse
, and returnsfalse
if the operand istrue
.
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 << $y | 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 string and returns the combined string.
Let’s take a look at the following example:
1 2 3 4 | <?php $str = 'This is ' . ' the string #' . 1; echo $str; |
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 function call that returns a value.
Let’s take a look at the following example:
1 2 3 4 5 | <?php $x = 10; $y = $x; $z = ($x = 20); // $z = 20 |
In the first expression, we assigned $x
variable value 10
. In the second one, we assigned 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
/=
- multiply-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. Each operator is assigned a precedence. Some operators has equal precedence e.g., precedence of the add( +
) and subtract( -
) is equal, however some operators have higher precedence than other e.g., precedence of the multiply operator ( *
) is higher than the precedence of the add( +
) and the subtract ( -
) operators.
Let’s take a look at the following example:
1 2 3 | <?php echo 4 + 5 * 3; // 19 |
Because the precedence of the multiply operator ( *
) is higher than the precedence of the add( +
) operator, PHP evaluates the multiply operator ( *
) first and then add operator ( *
) second.
To force the evaluation of an expression in a particular order you put the expression inside parentheses ()
, for example:
1 2 3 | <?php echo (4 + 5) * 3; // 27 |
In this tutorial, we have shown you the most commonly used PHP operators and discussed about the operators precedence.