Complete Guide and Tutorials for PHP Operators with example

What is Operators in PHP?

Operators are symbols which tell the PHP processor that certain actions are performed. The Add (+) symbol, for example, is an operator who tells PHP to add two variables or values, while the larger (>) symbol is an operator who tells PHP to match two values. Do not presume that operators are functions alike. Such operators may be used instead of functioning (such as the PHP ternary).

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Spaceship operators

Arithmetic operators:-

The arithmetic operator is used for basic arithmetical operations like adding, removing, multiplying, etc. These operators can be used to execute simple arithmetic behaviour of numerical values. The following table shows how the results are used and what results are obtained.

  • Operator Name Example Result
  • + Addition $x + $y Sum of $x and $y
  • Subtraction $x - $y Difference of $x and $y
  • * Multiplication $x * $y Product of $x and $y
  • Division $x / $y Quotient of $x and $y
  • Modulus $x % $y Remainder of $x divided by $y
  • ** Exponentiation $x ** $y Result of raising $x to the $y'th power (Introduced in PHP 5.6)

Example:

Output:-

Assignment operators:-

To assign values to variables, PHP Assignment operators are used. The left side of the operand variable is replaced with the right side value. Variables are assigned by operators in this group. The most common assignment operator is =, which assigns the right side of the operand to the variable on the left.

  • Assignment Same as… Description
  • x = y x = y The left operand gets set to the value of the expression on the right
  • x += y x = x + y Addition
  • x -= y x = x - y Subtraction
  • x *= y x = x * y Multiplication
  • x /= y x = x / y Division
  • x %= y x = x % y Modulus

Example:-

Output:-

Comparison operators:-

The operator for a comparison indicates the relationship shared by a number or a series, by two or more values. These php operators are simply used to compare the numbers or strings of the two values. Here we will instruct you how to use php operators to compare them. This type of PHP operator is essential to search for any inconsistencies or correlations between values and variables. It functions as booleans, returning the True or false value. For example, if the two variables you compares are not equal in value, using PHP not equal to the operator would show real.

  • Operator Name Example Result
  • == Equal $x == $y Returns true if $x is equal to $y
  • === Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
  • != Not equal $x != $y Returns true if $x is not equal to $y
  • <> Not equal $x <> $y Returns true if $x is not equal to $y
  • !== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
  • > Greater than $x > $y Returns true if $x is greater than $y
  • < Less than $x < $y Returns true if $x is less than $y
  • >= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
  • <= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y

Example:-

Outputs:-

Increment/Decrement operators:-

These operators increase the value of a vector (increase) or decrease it (decrease). Often they are referred to as uniform operators since only one operator works. Operators for increasing and decreasing are employed to constantly increase or decrease a value by a factor of 1. Operator for pre-increment When added to the value, the new $x value is returned. For a clearer idea see the table below:

  • Operator Name Description
  • ++$x Pre-increment Increments $x by one, then returns $x
  • $x++ Post-increment Returns $x, then increments $x by one
  • --$x Pre-decrement Decrements $x by one, then returns $x
  • $x-- Post-decrement Returns $x, then decrements $x by one

Example:-

Output:-

Logical operators:-

They mix conditionals and are used mostly to track various conditions simultaneously. The PHP (or) operator, for example, tests that at least one in two is valid. You can use the PHP (and) operator if you want to verify if they are both valid. See the following table for clarifications:

  • Operator Name Example Result
  • and And $x and $y True if both $x and $y are true
  • or Or $x or $y True if either $x or $y is true
  • xor Xor $x xor $y True if either $x or $y is true, but not both
  • && And $x && $y True if both $x and $y are true
  • || Or $x || $y True if either $x or $y is true
  • ! Not !$x True if $x is not true

Example:-

Output:-

String operators:-

Two string operators are available. The use of numerical values could produce unpredictable results:

  1. The operator of PHP (.) is also known as the operator of concatenation. It connects individual strings.
  2. The operator of PHP (.=) is known as the operator of the combined assignment. It attaches the argument to the one on the left on the right page.
  • Operator Name Example Result
  • Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
  • .= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1

Example:-

Output:-

Array operators:-

For comparing arrays, the array operators are used. Use the operators mentioned below to compare different arrays. Be sure the right grammar is used.

  • Operator Name Example Result
  • + Union $x + $y Union of $x and $y
  • == Equality $x == $y Returns true if $x and $y have the same key/value pairs
  • === Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types
  • != Inequality $x != $y Returns true if $x is not equal to $y
  • <> Inequality $x <> $y Returns true if $x is not equal to $y
  • !== Non-identity $x !== $y Returns true if $x is not identical to $y

Example:-

Output:-

Spaceship operators:-

PHP 7 adds a new spacecraft operator (<=>) that can be used to compare two terms. The combined contrast operator is also known.

If the two operands are identical, the spacecraft operator returns 0, 1 if the left is larger, and -1 if the right is larger. The three-way comparison is essentially shown as seen in the next table:

Example:-

Output:-