English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use operators in PHP to manipulate variables and values or perform operations.
Operators are symbols that tell PHP to perform certain operations. For example, the plus sign (+The parentheses symbol tells PHP to add two variables or values, while the greater than symbol (> tells PHP to compare two values.
The following list describes the different operators used in PHP.
Arithmetic operators are used to perform common arithmetic operations, such as addition, subtraction, and multiplication. This is a complete list of PHP arithmetic operators:
operator | Description | Online Examples | Result |
---|---|---|---|
+ | Addition | $x + $y | The sum of $x and $y |
- | Subtraction | $x - $y | The difference between $x and $y |
* | Multiplication | $x * $y | The product of $x and $y |
/ | Division | $x / $y | The quotient of $x and $y |
% | Modulus | $x % $y | The remainder of $x divided by $y |
The following examples will show you the effects of these arithmetic operators:
<?php $x = 10; $y = 4; echo($x + $y) . "<br>"; // Output: 14 echo($x - $y) . "<br>"; // Output: 6 echo($x * $y) . "<br>"; // Output: 40 echo($x / $y) . "<br>"; // Output: 2.5 echo($x % $y) . "<br>"; // Output: 2 ?>Test and see‹/›
Assignment operators are used to assign values to variables.
operator | Description | Online Examples | Identical to |
---|---|---|---|
= | Assignment | $x = $y | $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 (the remainder of division) | $x %= $y | $x = $x % $y |
.= | String concatenation | $x .= $y | $x = $x.$y |
The following examples will show you the effects of these assignment operators:
<?php $x = 25; $y = 35; $z = "25"; var_dump($x == $z) . "<br>"; // Output: Boolean value true var_dump($x === $z) . "<br>"; // Output: Boolean value false var_dump($x != $y) . "<br>"; // Output: Boolean value true var_dump($x !== $z) . "<br>"; // Output: Boolean value true var_dump($x < $y) . "<br>"; // Output: Boolean value true var_dump($x > $y) . "<br>"; // Output: Boolean value false var_dump($x <= $y) . "<br>"; // Output: Boolean value true var_dump($x >= $y) . "<br>"; // Output: Boolean value false ?>Test and see‹/›
Increment/The decrement operator is used for increment/Decrement the value of the variable.
operator | Name | Description |
---|---|---|
++$x | Pre-increment | Increment $x1Then return $x |
$x++ | Post-increment | Return $x, then add $x1 |
--$x | Decrement | Subtract one from $x, then return $x |
$x-- | After decrement | Return $x, then subtract $x1 |
The following examples will show you the effects of these increment and decrement operators:
<?php $x = 10; echo ++$x ; // Output: 11 echo $x; // Output: 11 $x = 10; echo $x++; // Output: 10 echo $x; // Output: 11 $x = 10; echo --$x; // Output: 9 echo $x; // Output: 9 $x = 10; echo $x--; // Output: 10 echo $x; // Output: 9 ?>Test and see‹/›
Logical operators are usually used to combine conditional statements.
operator | Name | Online Examples | Result |
---|---|---|---|
And | And | $x and $y | If $x and $y are both true, then it is true |
Or | Or | $x or $y | If $x or $y is true, then it is true |
xor | XOR | $x xor $y | If $x or $y is true, then True, but not both true |
&& | And | $x && $y | If $x and $y are both true, then it is true |
|| | Or | $x || $y | If $$x or $y is true, then it is true |
! | Not | !$x | If $x is not true, then it is true |
The following examples will show you the effects of these logical operators:
<?php $year = 2014; //Leap years can be divided by400 divisible, but also divisible by4Divisible, but not divisible by10Divisible by 0. if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){ echo "$year is a leap year."; } else{ echo "$year is not a leap year."; } ?>Test and see‹/›
There are two special ones forStringOperators designed for
operator | Description | Online Examples | Result |
---|---|---|---|
. | Chaining | $str1 . $str2 | $str1And $str2Concatenation |
.= | Concatenation Assignment | $str1 .= $str2 | Assign $str2Append to $str1 |
The following examples will show you the effects of these string operators:
<?php $x = "Hello"; $y = " World!"; echo $x . $y; // Output: Hello World! $x .= $y; echo $x; // Output: Hello World! ?>Test and see‹/›
Array operators are used to compare arrays:
operator | Name | Online Examples | Result |
---|---|---|---|
+ | Set | $x + $y | The union of $x and $y |
== | Equal | $x == $y | If $x and $y have the same keys/Value pair, then it is true |
=== | Equal | $x === $y | If $x and $y have the same order and the same type of the same key/Value pair, then it is true |
!= | Not equal | $x != $y | If $x is not equal to $y, then it is true |
<> | Not equal | $x <> $y | If $x is not equal to $y, then it is true |
!== | Not equal | $x !== $y | If $x is not equal to $y, then it is true |
The following examples will show you the effects of these array operators:
<?php $x = array("a" => "Red", "b" => "Green", "c" => "Blue"); $y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink"); $z = $x + $y; //$x and $y concatenation var_dump($z); var_dump($x == $y); // Output: Boolean value false var_dump($x === $y); // Output: Boolean value false var_dump($x != $y); // Output: Boolean value true var_dump($x <> $y); // Output: Boolean value true var_dump($x !== $y); // Output: Boolean value true ?>Test and see‹/›
PHP 7An new spaceship operator (<=>) has been introduced, which can be used to compare two expressions. Also known as the composite comparison operator.
If both operands are equal, the spaceship operator returns 0, if the left side is greater than1, then return1, if the right side is greater than1, then return-1. It basically provides three-way comparison, as shown in the table below:
operator | <=> is equivalent to |
---|---|
$x < $y | ($x <=> $y) === -1 |
$x <= $y | ($x <=> $y) === -1 || ($x <=> $y) === 0 |
$x == $y | ($x <=> $y) === 0 |
$x != $y | ($x <=> $y) !== 0 |
$x >= $y | ($x <=> $y) === 1 || ($x <=> $y) === 0 |
$x > $y | ($x <=> $y) === 1 |
The following examples will show you how the spaceship operator works in practice:
<?php // Compare Integers echo 1 <=> 1; // Output: 0 echo 1 <=> 2; // Output: -1 echo 2 <=> 1; // Output: 1 // Compare Floating Points echo 1.5 <=> 1.5; // Output: 0 echo 1.5 <=> 2.5; // Output: -1 echo 2.5 <=> 1.5; // Output: 1 // Compare Strings echo "x" <=> "x"; // Output: 0 echo "x" <=> "y"; // Output: -1 echo "y" <=> "x"; // Output: 1 ?>Test and see‹/›