PHP Operators

Summary: in this tutorial, you will learn how to use PHP operators so that you can use them to manipulate data in your script.

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

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 ( ==) returns 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.
  • Inequality( != or <>) returns true if both operands are not equal, otherwise return false.
  • Not identical ( !===) returns true if both operands are not equal or not have the same data type, otherwise returns false.
  • Greater than ( >) returns true if the operand on the left  is greater than operand on the right, otherwise returns false.
  • Greater than or equal ( >=) returns true if the operand on the left  is greater than or equal to operand on the right, otherwise returns false.
  • Less than ( <) returns true if the operand on the left  is less than operand on the right, otherwise returns false.
  • Less than or equal ( <=) returns true if the operand on the left  is less than or equal to operand on the right, otherwise returns false.

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) returns true if both operands are true, otherwise returns 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 ( ||, 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.
  • Logical XOR ( xor) returns true if either operand, not both, is true, otherwise returns false.
  • Logical negation ( !) 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:

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 string and returns the combined string.

Let’s take a look at the following example:

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:

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:

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:

In this tutorial, we have shown you the most commonly used PHP operators and discussed about the operators precedence.

  • Was this tutorial helpful ?
  • YesNo