THE WORLD'S LARGEST WEB DEVELOPER SITE

PHP 5 Operators


PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

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

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

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

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

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


PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

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

PHP 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.

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

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

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

PHP String Operators

PHP has two operators that are specially designed for strings.

Operator Name Example Result Show it
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2 Show it »
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1 Show it »

PHP Array Operators

The PHP array operators are used to compare arrays.

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

Test Yourself With Exercises

Exercise:

Multiply 10 with 5, and output the result.

echo 10  5;