Operators
This video was created by Dani Krossing
Operators are symbols that take one or more values or expression and yields another value. There are many categories of operators, but for now we will cover the following:
- Arithetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Incrementing/Decrementing Operators
- String Operators
Arithmetic Operators
Arithmetic operators are used to perform basic arithmetic, including addition, subtraction, multiplication, and division.
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | $a + $b | Sum of $a and $b |
- | Subtraction | $a - $b | Difference of $a and $b |
* | Multiplication | $a * $b | Product of $a and $b |
/ | Division | $a / $b | Divides $a by $b |
% | Modulo | $a % $b | Remainder of $a divided by $b |
<?php
$a = 4;
$b = 3;
echo $a + $b; // 7
echo $a - $b; // 1
echo $a * $b; // 12
echo $a / $b; // 1.3333333333
echo $a % $b; // 1
?>
Assignment Operators
Assignment operators are used to assign a value to a variable. The most common assignment operator is the =
. There are also combined operators which are used to perform arithmetic, array unions, and string concatenations while also assigning the value to a variable.
Operator | Name | Example | Result | Equivalent |
---|---|---|---|---|
= | Basic assignment | $a = $b | $a will be set to the value of $b | |
+= | Addition assignment | $a += $b | $a will be set to the difference of $a and $b | $a = $a + $b |
-= | Subtration assignment | $a -= $b | $a will be set to the sum of $a and $b | $a = $a - $b |
*= | Multiplication assignment | $a *= $b | $a will be set to the product of $a and $b | $a = $a * $b |
/= | Division assignment | $a /= $b | $a will be set to the division of $a by $b | $a = $a / $b |
%= | Modulo assignment | $a %= $b | $a will be set to the remainder of $a divided by $b | $a = $a % $b |
.= | String concatentation assignment | $a .= $b | $a will be set to the concatenation of $a and $b | $a = $a . $b |
Comparison Operators
Comparison Operators are used to compare two values. When comparing values of different data types, for example, a string and a number, PHP will attempt to convert the string to a number first and then do the comparison.
Operator | Name | Example | Result |
---|---|---|---|
== | Equal | $a == $b | TRUE if $a is equal to $b in value |
=== | Identical | $a === $b | TRUE if $a is equal to $b in value and data type |
!= | Not Equal | $a != $b | TRUE if $a is not equal to $b in value |
!== | Not Identical | $a !== $b | TRUE if $a is not equal to $b in value and data type |
< | Less than | $a < $b | TRUE if $a is less than $b |
> | Greater than | $a > $b | TRUE if $a is greater than $b |
<= | Less than or equal to | $a <= $b | TRUE if $a is less than or equal to $b |
>= | Greater than or equal to | $a >= $b | TRUE if $a is greater than equal to $b |
<=> | Spaceship | $a <=> $b | Returns 0 if $a is equal to $b Returns 1 if $a is greater than $b Returns -1 if $a is less than $b |
<?php
$a = 4;
$b = 3;
$c = 1;
$d = 20;
var_dump($c == 1); // bool(true)
var_dump($c == "1"); // bool(true)
var_dump($c === 1); // bool(true)
var_dump($c === "1"); // bool(false)
var_dump($c != 1); // bool(false)
var_dump($c != "1"); // bool(false)
var_dump($c !== 1); // bool(false)
var_dump($c !== "1"); // bool(true)
var_dump($a > $b); // bool(true)
var_dump($c < $b); // bool(true)
var_dump($d < $d); // bool(false)
var_dump($d <= $d); // bool(true)
var_dump($d >= $b); // bool(true)
var_dump($a <=> $b); // int(1)
var_dump($a <=> $d); // int(-1)
var_dump($a <=> $a); // int(0)
?>
Logical Operators
Logical Operators are used to combine and make more complex expressions.
Operator | Name | Example | Result |
---|---|---|---|
! | Not | !$a | TRUE if $a is not TRUE |
&& | And | $a && $b | TRUE if both $a and $b are TRUE. |
|| | Or | $a || $b | TRUE if either $a or $b is TRUE . |
Caution
It is also possible to use and
and or
in place of &&
and ||
. But these operators have different precedence and can result in unexpected results. Review the Logical Operators documentation for more information.
<?php
$a = 4;
$b = 3;
$c = 1;
$d = 20;
// this condition will be FALSE
if (($a > $b) && ($c > $d)) {
echo "a is larger than b AND ";
echo "c is larger than d";
}
// this condition will be TRUE
if (($a > $b) || ($c > $d)) {
echo "a is larger than b OR ";
echo "c is larger than d";
}
// setting e if it is not set
if (!isset($e)) {
$e = 200;
}
echo $e;
?>
Incrementing/Decrementing Operators
Incrementing/Decrementing operators supports pre- and post-increment and decrement operators.
Operator | Name | Example | Result |
---|---|---|---|
++ | Pre-increment | ++$a | Increments $a by one, then returns $a |
++ | Post-increment | $a++ | Returns $a , then increments $a by one |
-- | Pre-increment | --$a | Decrements $a by one, then returns $a |
-- | Post-increment | $a-- | Returns $a , then decrements $a by one |
String Operators
PHP only supports two String Operators, the concatenating assignment operator (.=
) mentioned above, and the concatenation operator, which is used to combine strings.
<?php
$a = "Hello ";
$b = "World";
echo $a . $b; // Hello World
?>