PHP

PHP Operators


 

 

Operator is a symbol that represents an action to be performed on one or more operands. Operands are values or variables on which the operator acts.

PHP supports following type of operators:

  • Arithmetic Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Assignment Operators
  • Conditional (or ternary) Operators
  • Increment / Decrement Operators

 

1. Arithmetic Operators

It is used to perform common arithmetic operations such as multiplication, subtraction, addition, etc. with numeric values.

 

Operator

Name

Example

Explanation

+Addintion$x + $ySum of operands
-   Subtraction$x - $yDifference of operands
Multiplication $x * $y Product of operands
Division $x / $y Quotient of operands
Modulus $x % $y Remainder of operands
**  Exponentiatio$x ** $y $x raised to the power $y

 

 

2. Comparison Operators

The Comparison operators are used to compare two values.

 

Operator Name Example Explanation
== Equal $x == $y Return TRUE if $x is equal to $y
===identical$x === $y Return TRUE if $x is equal to $y, and they are of same data type
 
!==Not identical$x !== $y Return TRUE if $x is not equal to $y, and they are not of same data type
!=Not equal$x != $y Return TRUE if $x is not equal to $y
<>Not equal$x <> $y Return TRUE if $x is not equal to $y
<Less than$x < $y Return TRUE if $x is less than $y
>Greater than$x > $y Return TRUE if $x is greater than $y
<=Less than or equal to$x <= $y Return TRUE if $x is less than or equal $y
>=Greater than or equal to$x >= $y Return TRUE if $x is greater than or equal $y
<=>Spaceship$x <=>$y Return -1 if $x is less than $y
Return 0 if $x is equal $y
Return 1 if $x is greater than $y

 

3. Logical Operators:

Logical operators are used to perform logical operations on Boolean values.

Operator Name Example Explanation
and And $x and $y Return TRUE if both $x and $y are true
Or Or $x or $yReturn TRUE if either $x or $y is true
xor Xor $x xor $y Return TRUE if either $x or $y is true but not both
Not ! $x Return TRUE if $x is not true
&& And $x && $y Return TRUE if either $x and $y are true
||  Or$x || $y Return TRUE if either $x or $y

 

4. Assignment Operators:

Assignment operators in PHP are used to assign values to variables. They take the value on the right-hand side and assign it to the variable on the left-hand side.

Operator

 

NameExampleExplanation
=

Assign

 

$x = $yThe value of right operand is assigned to the left operand.
+=Add then Assign$x += $yAddition same as $a = $x + $y
-=Subtract then Assign$x -= $ySubtraction same as $x = $x - $y
*=Multiply then Assign$x *= $yMultiplication same as $x = $x * $y
/=Divide then Assign
(quotient)
$x /= $yFind quotient same as $x = $x / $y
%=Divide then Assign
(remainder)

$x %= $y

 

Find remainder same as $x = $x % $y

 

5. Conditional Operators:

Conditional operators, also known as ternary operators, are a concise way to express conditional statements in PHP. It is a shorthand of an if-else statement and are often used for simple decision-making in a single line of code.

 

OperatorNameExampleExplanation

?:

 

 

 

Ternary$result = ($condition)? $x_if_true: $y_if_falseReturns the value of $result.
The value of $result is $x_if_true if $condition = TRUE.
The value of $ result is $y_if_false if $condition = FALSE

??

 

 

 

Null coalescing$result = $x_if_true: $y_if_falseReturns the value of $ result.
The value of $result is $x_if_true if $x_if_true exists, and is not NULL.
If $x_if_true does not exist, or is NULL, the value of $result is $y_if_false.

 

6. Increment / Decrement Operators

The PHP increment operators are used to increment a variable's value.

The PHP decrement operators are used to decrement a variable's value.

 

OperatorNameExampleExplanation
++$xPre-increment++$yIncrements $y by one, then returns $y
$x++Post-increment$y++Returns $y, then increments $y by one
--$xPre-decrement--$yDecrements $y by one, then returns $y
$x--Post-decrement$y--Returns $y, then decrements $y by one