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 | Sum of $a and $b | |
- | Subtraction | Difference of $a and $b | |
* | Multiplication | Product of $a and $b | |
/ | Division | Divides $a by $b | |
% | Modulo | 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 will be set to the value of $b | ||
+= | Addition assignment | $a will be set to the difference of $a and $b | ||
-= | Subtration assignment | $a will be set to the sum of $a and $b | ||
*= | Multiplication assignment | $a will be set to the product of $a and $b | ||
/= | Division assignment | $a will be set to the division of $a by $b | ||
%= | Modulo assignment | $a will be set to the remainder of $a divided by $b | ||
.= | String concatentation assignment | $a will be set to the concatenation of $a and $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 | TRUE if $a is equal to $b in value | |
=== | Identical | TRUE if $a is equal to $b in value and data type | |
!= | Not Equal | TRUE if $a is not equal to $b in value | |
!== | Not Identical | TRUE if $a is not equal to $b in value and data type | |
< | Less than | TRUE if $a is less than $b | |
> | Greater than | TRUE if $a is greater than $b | |
<= | Less than or equal to | TRUE if $a is less than or equal to $b | |
>= | Greater than or equal to | TRUE if $a is greater than equal to $b | |
<=> | Spaceship | 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 | TRUE if $a is not TRUE | |
&& | And | TRUE if both $a and $b are TRUE. | |
` | ` | Or |
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 | Increments $a by one, then returns $a | |
++ | Post-increment | Returns $a , then increments $a by one | |
-- | Pre-increment | Decrements $a by one, then returns $a | |
-- | Post-increment | 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
?>