PHP Operators Tutorial

In this tutorial we learn more about symbols that act as operators in PHP and allow us to perform various operations throughout our application.

These operations include arithmetic, assignment, compound assignment, comparison, logical and more.

Here's a table of contents of what you'll learn in this lesson:
(click on a link to skip to its section)

Let's jump right in.

What are operators?

Operators are symbols that have special meaning in PHP, and they indicate the types of operations that can be done.

As an example, we can consider the + symbol as an operator. It adds whatever number is on the right to the number on the left. This is an operation that is taking place on two operands.

PHP categorizes operators into different types:

  1. Array operators
  2. Arithmetic operators
  3. Assignment operators
  4. Conditional assignment operators
  5. Comparison operators
  6. Incremental operators
  7. Logical operators
  8. String operators

Array operators

Array operators are used to compare arrays. The array operators below may not make any sense at this point, if you are a beginner. We will cover these operators again in the tutorial lesson on arrays.

The following table lists array operators in PHP.

OperatorNameDescription
+UnionThe union of two arrays
==EqualityReturns true if both arrays have the same value pairs.
===IdentityReturns true if both arrays have the same value pairs in the same order and of the same types
!=InequalityReturns true if both arrays do not have the same value pairs
<>InequalityReturns true if both arrays do not have the same value pairs
!==Non-identityReturns true if both arrays do not have the same value pairs in the same order and of the same types

Arithmetic operators

Arithmetic operators are used with numerical values to perform common mathematical arithmetic.

The following table lists arithmetic operators in PHP.

OperatorNameDescription
+AdditionAdds the right operand to the left operand
-SubtractionSubtracts the right operand from the left operand
*MultiplicationMultiplies the left operand with the right operand
/DivisionDivide the left operand by the right operand
%ModulusRemainder of the division of the left operand by the right operand
**ExponentRaise the left operand to the power of the right operand

Example: arithmetic operators
<?php

$br = "<br>";
$a = 3;
$b = 4;

// Addition
echo $a + $b;
echo $br;

// Subtraction
echo $b - $a;
echo $br;

// Multiplicaion
echo $a * $b;
echo $br;

// Division
echo $b / $a;
echo $br;

// Modulus
echo $b % $a;
echo $br;

// Exponent
echo $b ** $a;
echo $br;

?>

Assignment operators

Assignment operators are used with numerical values to assign a value to a variable.

The following table lists assignment operators in PHP.

AssignmentSame asDescription
=a = bThe left operand is assigned the value of the operand or expression on the right
+=a = a + bThe left operand is assigned the value of itself added to the right operand
-=a = a - bThe left operand is assigned the value of the right operand subtracted from itself
*=a = a * bThe left operand is assigned the value of itself multiplied by the right operand
/=a = a / bThe left operand is assigned the value of itself divided by the right operand
%=a = a % bThe left operand is assigned the value of the remainder of division of itself divided by the right operand

Example: assignment operators
<?php

// Assignment
$br = "<br>";
$a = 3;

// Addition Assignment
echo $a += 10;
echo $br;

// Subtraction Assignment
echo $a -= 2;
echo $br;

// Multiplicaion Assignment
echo $a *= 5;
echo $br;

// Division Assignment
echo $a /= 1;
echo $br;

// Modulus Assignment
echo $a %= 1;
echo $br;

?>

Conditional assignment operators

Conditional assignment operators will set values depending on the outcome of certain conditions. The conditional operators below may not make any sense at this point, if you are a beginner. We will cover them again in the tutorial lesson on conditional control flow.

The following table lists conditional assignment operators in PHP.

OperatorNameExampleDescription
? :Ternary$a = expression ? expT : expFIf the expression is true, $a will be the value of expT
If the expression is false, $a will be the value of expF
? ?Null coalescing$a = exp1 ?? exp2If exp1 exists and is not NULL, $a will be the value of exp1
If exp1 does not exist or its value is null, $a will be the value of exp2

The null coalescing operator is not available in versions below PHP v7.

Comparison operators

Comparison operators will compare two values against each other and return a true or false boolean value.

The following table lists comparison operators in PHP.

OperatorNameDescription
==EqualReturns true if the right operand is the same as the left operand
===IdenticalReturns true if the right operand is the same as the left operand and both are of the same type
!=Not equalReturns true if the right operand is not the same as the left operand
<>Not equalSame as above
!==Not identicalReturns true if the right operand is not the same as the left operand or if they are both not of the same type
>Greater thanReturns true if the left operand is greater than the right operand
<Less thanReturns true if the left operand is less than the right operand
>=Greater than or equal toReturns true if the left operand is greater than or equal to the right operand
<=Less than or equal toReturns true if the left operand is less than or equal to the right operand

It is important for programmers coming from Javascript to remember that three equal signs === represents evaluating both equal value and equal type, and not just equal value.

Example: comparison operators
<?php

$br = "<br>";

// Equal
echo 5 == 5;
echo $br;

// Identical
echo "A" === "A";
echo $br;

// Not equal
echo 3 != 4;
echo $br;

// Not identical
echo 3 !== "Three";
echo $br;

// Greater than
echo 100 > 99;
echo $br;

// Less than
echo 30 < 40;
echo $br;

// Greater than or equal to
echo 2 >= 2;
echo $br;

// Less than or equal to
echo 6 <= 6;
echo $br;

?>

Incremental operators

Incremental operators are used to increase a value incrementally, and decremental operators are used to decrease a value. These operators are what is known as unary operators and as such only require one operand.

The following table lists incremental and decremental operators in PHP.

OperatorNameDescription
++$aPre-incrementIncrement by one, then return $a
$a++Post-incrementReturn $a, then increment by one
--$aPre-decrementDecrement by one, then return $a
$a--Post-decrementReturn $a, then decrement by one
Example: increment and decrement operators
<?php

$br = "<br>";
$a = 1;

echo $a;
echo $br;

// Increment and return value
echo ++$a;
echo $br;

// Return value, then increment
echo $a++;
echo $br;
echo $a;
echo $br;

// Decrement and return value
echo --$a;
echo $br;

// Return the value, then decrement
echo $a--;
echo $br;
echo $a;

?>

In the example above we can see that in both pre and post increments the value is incremented. The choice of which to use is determined by which value we want to work with:

  • If we want to work with the already incremented value, we choose the pre-increment.
  • If we want to work with the value before it is incremented and increment it afterwards, we choose the post-increment.

In most situations it won’t matter because we only want the value to actually increment, and not specifically use the value. We will look into increments and decrements more in the tutorial lesson on looping control flow.

Logical operators

Logical operators are used to combine conditional evaluations. We will work with them again in the tutorial lesson on conditional control flow.

The following table lists logical operators in PHP.

OperatorNameDescription
andAndReturns true if one expression and another is true
orOrReturns true if one expression or another is true, or both expressions are true
xorXorReturns true if one expression or another is true, but not both
&&AndReturns true if one expression and another is true
||OrReturns true if one expression or another is true, or both expressions are true
!NotReturns true if the expression immediately following is not true
Example: logical operators
<?php

$br = "<br>";

// And
echo 1 == 1 and 2 == 2;
echo $br;

// Or
echo 1 == 1 or 2 == 2;
echo $br;

// Xor
echo 1 == 2 or 2 == 2;
echo $br;

// && (and)
echo 1 == 1 and 2 == 2;
echo $br;

// || (or)
echo 1 == 1 or 2 == 2;
echo $br;

// ! (not)
echo !false;

?>

String operators

String operators are operators that combine (concatenate) strings together.

The following table lists string operators in PHP.

OperatorNameDescription
.ConcatenationCombine one string with another
.=Concatenate and assignAppend one string to another
Example: string operators
<?php

$br = "<br>";
$str1 = "Hello";
$str2 = "there";

// Concatenation
echo $str1 . " " . $str2;
echo $br;

// Append
echo $str1 .= $str2;

?>

Summary: Points to remember

  • Operators are like keywords that have special meaning in PHP, and indicate operations that can be done.
  • The null coalescing operator ? ? is not available in versions below PHP 7.
  • Javascript programmers should note that === means evaluating both equal value and equal type, and not only equal value.
  • These operators will be used more throughout the tutorial series, and will become much clearer for you.