Operators are symbols that instruct the PHP processor to carry out specific actions. For example, the addition (+
) symbol instructs PHP to add two variables or values, whereas the greater-than (>
) symbol instructs PHP to compare two values.
PHP operators are grouped as follows:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
Arithmetic Operators
The PHP arithmetic operators are used in conjunction with numeric values to perform common arithmetic operations such as addition, subtraction, multiplication, and so on.
Operator | Name | Example | Output |
---|---|---|---|
+ |
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 |
Sample usage of Arithmetic Operators:
<?php
$x = 10;
$y = 4;
echo($x + $y) . "<br>"; // 0utputs: 14
echo($x - $y) . "<br>"; // 0utputs: 6
echo($x * $y) . "<br>"; // 0utputs: 40
echo($x / $y) . "<br>"; // 0utputs: 2.5
echo($x % $y) . "<br>"; // 0utputs: 2
Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Name | Example | Output |
---|---|---|---|
= |
Assign | $x = $y | The left operand gets set to the value of the expression on the right |
+= |
Add and assign | $x += $y | Addition |
-= |
Subtract and assign | $x -= $y | Subtraction |
*= |
Multiply and assign | $x *= $y | Multiplication |
/= |
Divide and assign quotient | $x /= $y | Division |
%= |
Divide and assign modulus | Modulus |
Sample usage of Assignment Operators:
<?php
$x = 10;
echo $x . "<br>"; // Outputs: 10
$x = 20;
$x += 30;
echo $x . "<br>"; // Outputs: 50
$x = 50;
$x -= 20;
echo $x . "<br>"; // Outputs: 30
$x = 5;
$x *= 25;
echo $x . "<br>"; // Outputs: 125
$x = 50;
$x /= 10;
echo $x . "<br>"; // Outputs: 5
$x = 100;
$x %= 15;
echo $x . "<br>"; // Outputs: 10
Comparison Operators
Comparison operators are used to compare two values in a Boolean fashion.
Operator | Name | Example | Output |
---|---|---|---|
== |
Equal | $x == $y | True if $x is equal to $y |
=== |
Identical | $x === $y | True if $x is equal to $y, and they are of the same type |
!= |
Not equal | $x != $y | True if $x is not equal to $y |
<> |
Not equal | $x <> $y | True if $x is not equal to $y |
!== |
Not identical | $x !== $y | True if $x is not equal to $y, or they are not of the same type |
< |
Less than | $x < $y | True if $x is less than $y |
> |
Greater than | $x > $y | True if $x is greater than $y |
>= |
Greater than or equal to | $x >= $y | True if $x is greater than or equal to $y |
<= |
Less than or equal to | $x <= $y | True if $x is less than or equal to $y |
Sample usage of Comparison Operators:
<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z); // Outputs: boolean true
echo "<br>";
var_dump($x === $z); // Outputs: boolean false
echo "<br>";
var_dump($x != $y); // Outputs: boolean true
echo "<br>";
var_dump($x !== $z); // Outputs: boolean true
echo "<br>";
var_dump($x < $y); // Outputs: boolean true
echo "<br>";
var_dump($x > $y); // Outputs: boolean false
echo "<br>";
var_dump($x <= $y); // Outputs: boolean true
echo "<br>";
var_dump($x >= $y); // Outputs: boolean false
echo "<br>";
Increment / Decrement Operators
Increment operators are used to increment a variable's value while decrement operators are used to decrement.
Operator | Name | Output |
---|---|---|
++$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 |
Sample usage of Increment / Decrement Operators:
<?php
$x = 10;
echo ++$x . "<br>"; // Outputs: 11
echo $x . "<br>"; // Outputs: 11
$x = 10;
echo $x++ . "<br>"; // Outputs: 10
echo $x . "<br>"; // Outputs: 11
$x = 10;
echo --$x . "<br>"; // Outputs: 9
echo $x . "<br>"; // Outputs: 9
$x = 10;
echo $x-- . "<br>"; // Outputs: 10
echo $x . "<br>"; // Outputs: 9
Logical Operators
Logical operators are typically used to combine conditional statements.
Operator | Name | Example | Output |
---|---|---|---|
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 | |
! |
Not | !$x | true if $x is not true |
Sample usage of Logical Operators:
<?php
$year = 2021;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
echo "$year is a leap year.";
} else{
echo "$year is not a leap year.";
}
String Operators
String operators are specifically designed for strings.
Operator | Name | Example | Output |
---|---|---|---|
. |
Concatenation | $str1 . $str2 | Concatenation of $str1 and $str2 |
.= |
Concatenation assignment | $str1 .= $str2 | Appends the $str2 to the $str1 |
Sample usage of String Operators:
<?php
$x = "Hello";
$y = " World!";
echo $x . $y . "<br>"; // Outputs: Hello World!
$x .= $y;
echo $x . "<br>"; // Outputs: Hello World!
Array Operators
Array operators are used to compare arrays.
Operator | Name | Example | Output |
---|---|---|---|
+ |
Union | $x + $y | Union of $x and $y |
== |
Equality | $x == $y | True if $x and $y have the same key/value pairs |
=== |
Identity | $x === $y | True if $x and $y have the same key/value pairs in the same order and of the same types |
!= |
Inequality | $x != $y | True if $x is not equal to $y |
<> |
Inequality | $x <> $y | True if $x is not equal to $y |
!== |
Non-identity | $x !== $y | True if $x is not identical to $y |
Sample usage of String Operators:
<?php
$x = array("a" => "Red", "b" => "Green", "c" => "Blue");
$y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink");
$z = $x + $y; // Union of $x and $y
var_dump($z);
echo "<br>";
var_dump($x == $y); // Outputs: boolean false
echo "<br>";
var_dump($x === $y); // Outputs: boolean false
echo "<br>";
var_dump($x != $y); // Outputs: boolean true
echo "<br>";
var_dump($x <> $y); // Outputs: boolean true
echo "<br>";
var_dump($x !== $y); // Outputs: boolean true
echo "<br>";
Conditional Assignment Operators
Conditional assignment operators are used to set a value depending on conditions.
Operator | Name | Example | Output |
---|---|---|---|
?: |
Ternary | $x = expr1 ? expr2 : expr3 | Returns the value of $x. The value of $x is expr2 if expr1 = true . The value of $x is expr3 if expr1 = false |
?? |
Null coalescing | $x = expr1 ?? expr2 | Returns the value of $x. The value of $x is expr1 if expr1 exists, and is not null . If expr1 does not exist, or is null , the value of $x is expr2. Introduced in PHP 7 |
Sample usage of Conditional Assignment Operators:
<?php
// if empty($user) = true, set $status = "anonymous"
echo $status = (empty($user)) ? "anonymous" : "logged in";
echo("<br>");
$user = "John Doe";
// if empty($user) = false, set $status = "logged in"
echo $status = (empty($user)) ? "anonymous" : "logged in";
echo "<br>";
// variable $user is the value of $_GET['user']
// and 'anonymous' if it does not exist
echo $user = $_GET["user"] ?? "anonymous";
echo("<br>");
// variable $color is "red" if $color does not exist or is null
echo $color = $color ?? "red";
echo "<br>";
Exercise
Create the following variables: $num1
, $num2
, $num3
. Assign the integer 3
to $num1
and 9
to $num2
. Multiply $num1
by $num2
and assign the product to $num3
. Print the result on $num3
<?php
$var = 123;
<?php
$num1 = 3;
$num2 = 9;
$num3 = $num1 * $num2;
echo $num3;
{
"test_output_contains":{
"expected":"27",
"error_message":"Did you assign the correct value for your variables?"
},
"test_variable_exists":[
{
"object":"$num1",
"error_message":"Have you declared <code>$num1<\/code>?"
},
{
"object":"$num2",
"error_message":"Have you declared <code>$num2<\/code>?"
},
{
"object":"$num3",
"error_message":"Have you declared <code>$num3<\/code>?"
}
],
"success_message":"Good job!",
"error_message":"Please read the instructions again."
}