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 their sum.

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:

OperatorNameDescription
+AdditionReturn the sum of two operands
SubtractionReturn the difference between two operands
*MultiplicationReturn the product of two operands
/DivisionReturn the quotient of two operands
%ModulusReturn the remainder of the division of the first operand by the second one

The following example uses the arithmetic operators:

<?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/>'; // 5Code language: PHP (php)

Try it

Comparison Operators #

Comparison operators allow you to compare two operands.

A comparison operator returns a Boolean value, either trueor 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:

OperatorNameDescription
==EqualityReturn trueif both operands are equal, otherwise returns false.
===IdentityReturn trueif both operands have the same data type and equal, otherwise return false.
!===Not identicalReturn trueif both operands are not equal or not have the same data type, otherwise returnfalse.
>Greater thanReturn trueif the operand on the left  is greater than the operand on the right, otherwise return false.
>=Greater than or equal toReturn trueif the operand on the left  is greater than or equal to the operand on the right, otherwise return false.
<Less thanReturn trueif the operand on the left is less than the operand on the right, otherwise return false.
<=Less than or equalReturn trueif 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:

OperatorNameDescription
&&Logical ANDReturn trueif 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 ORReturn trueif one of the operands is true, otherwise returns false. If the first operand is true, it will not evaluate the second one.
xorLogical XORReturn trueif either operand, not both, is true, otherwise, return false.
!Notreturns trueif the operand is false, and returns falseif the operand is true.

Bitwise Operators #

Bitwise operators perform operations on the binary representation of the operands. The following illustrates bitwise operators in PHP:

OperatorsNameResult
$x & $yAndIf both bits are 1, the corresponding bit in the result is 1; otherwise, the corresponding bit is 0
$x | $yOr (inclusive or)If both bits are 0, the corresponding bit in the result is 0; otherwise, the corresponding bit is 1
$x ^ $yXor (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
~ $xNotChange bit 1 to 0 and 0 to 1 in the $x operand
$x << $yShift leftShifts the bits in $xleft by the number of places specified by $y.
$x >> $yShift rightShifts 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:

ExampleNameReturned ValueEffect on $a
++$aPre-increment$a + 1Increments $a by 1, then returns $a.
$a++Post-increment$aReturns $a, then increments $a by 1.
--$aPre-decrement$a - 1Decrements $a by 1, then returns $a.
$a--Post-decrement$aReturns $a, then decrements $a by 1.

Concatenating Operator #

The 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:

<?php
$str = 'PHP' . ' is ' . ' Awesome!';
echo $str;Code language: PHP (php)

Try it

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:

<?php

$x = 10;
$y = $x;
$z = ($x = 20); // $z = 20

echo $z;Code language: PHP (php)

Try it

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 20to $x. The assignment operator ( =) returns 20and then 20is 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:

<?php

echo 4 + 5 * 3; // 19Code language: PHP (php)

Try it

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:

<?php

echo (4 + 5) * 3; // 27Code language: PHP (php)

Try it

This tutorial briefly taught you about the most commonly used PHP operators.

Did you find this tutorial useful?