PHP Operators

Created with Sketch.

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:

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/>'; // 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:

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

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

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 $x left 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

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

<?php

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

<?php

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:

<?php

echo (4 + 5) * 3; // 27

Code language: HTML, XML (xml)

In this tutorial, you have briefly learned about the most commonly used PHP operators.

Leave a Reply

Your email address will not be published. Required fields are marked *