In PHP, operators are symbols or keywords used to perform operations on data values or variables. They are essential in programming as they enable developers to manipulate data and perform various calculations.
Table of Contents
Arithmetic Operators
Arithmetic operators in PHP are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.
- The addition operator (+) adds two values together.
- The subtraction operator (-) subtracts one value from another.
- The multiplication operator (*) multiplies two values.
- The division operator (/) divides one value by another.
- The modulus operator (%) returns the remainder of a division operation.
- The increment operator (++) increases a value by one.
- The decrement operator (–) decreases a value by one.
$a = 5;
$b = 10;
// Addition
echo $a + $b; // Output: 15
// Subtraction
echo $b - $a; // Output: 5
// Multiplication
echo $a * $b; // Output: 50
// Division
echo $b / $a; // Output: 2
// Modulus
echo $b % $a; // Output: 0
// Increment
echo ++$a; // Output: 6
// Decrement
echo --$b; // Output: 9
Assignment Operators
Assignment operators are used to assign values to variables. They include:
- = (Assign): assigns the value on the right to the variable on the left. Example:
$x = 5; // $x now holds the value 5
- += (Add and Assign): adds the value on the right to the variable on the left and assigns the result to the variable on the left. Example:
$x = 5;
$x += 2; // $x now holds the value 7
- -= (Subtract and Assign): subtracts the value on the right from the variable on the left and assigns the result to the variable on the left. Example:
$x = 5;
$x -= 2; // $x now holds the value 3
- *= (Multiply and Assign): multiplies the value on the right with the variable on the left and assigns the result to the variable on the left. Example:
$x = 5;
$x *= 2; // $x now holds the value 10
- /= (Divide and Assign): divides the variable on the left by the value on the right and assigns the result to the variable on the left. Example:
$x = 5;
$x /= 2; // $x now holds the value 2.5
- %= (Modulus and Assign): divides the variable on the left by the value on the right and assigns the remainder to the variable on the left. Example:
$x = 5;
$x %= 2; // $x now holds the value 1
Comparison Operators
Comparison operators are used to compare two values. They include:
- == (Equal to): checks if two values are equal.
$y = "5";
if ($x == $y) {
echo "Equal";
} else {
echo "Not Equal";
} // Output: Equal
- != (Not equal to): checks if two values are not equal.
$x = 5;
$y = "5";
if ($x != $y) {
echo "Not Equal";
} else {
echo "Equal";
} // Output: Not Equal
- (Greater than): checks if the value on the left is greater than the value on the right. Example:
$x = 5;
$y = 3;
if ($x > $y) {
echo "Greater";
} else {
echo "Smaller";
} // Output: Greater
- < (Less than): checks if the value on the left is less than the value on the right.
$x = 5;
$y = 3;
if ($x < $y) {
echo "Smaller";
} else {
echo "Greater";
} // Output: Greater
- = (Greater than or equal to): checks if the value on the left is greater than or equal to the value on the right.
$x = 5;
$y = 5;
if ($x >= $y) {
echo "Greater or Equal";
} else {
echo "Smaller";
} // Output: Greater or Equal
- <= (Less than or equal to): checks if the value on the left is less than or equal to the value on the right.
$x = 5;
$y = 5;
if ($x <= $y) {
echo "Smaller or Equal";
} else {
echo "Greater";
} // Output: Smaller or Equal
Logical Operators
Logical operators are used to combine two or more conditions. They include:
- and (And): returns true if both conditions are true.
$x = 5;
$y = 3;
if ($x > $y and $x < 10) {
echo "True";
} else {
echo "False";
} // Output: True
- or (Or): returns true if either of the conditions is true.
$x = 5;
$y = 3;
if ($x == $y or $x < 10) {
echo "True";
} else {
echo "False";
} // Output: True
- not (Not): returns true if the condition is not true.
$x = 5;
if (!($x == 3)) {
echo "True";
} else {
echo "False";
} // Output: True
- xor (Exclusive or): returns true if only one of the conditions is true.
$x = 5;
$y = 3;
if ($x == 5 xor $y == 4) {
echo "True";
} else {
echo "False";
} // Output: True
String Operators
String operators are used to concatenate strings. They include:
- . (Concatenation): concatenates two strings.
$x = "Hello";
$y = "World";
echo $x . " " . $y; // Output: Hello World
- .= (Concatenate and Assign): concatenates the string on the right to the string on the left and assigns the result to the string on the left.
$x = "Hello";
$x .= " World";
echo $x; // Output: Hello World
Array Operators
Array operators are used to manipulate arrays. They include:
- (Union): returns the union of two arrays.
$x = array("a" => "apple", "b" => "banana");
$y = array("c" => "cherry", "d" => "date");
$z = $x + $y;
print_r($z); // Output: Array ( [a] => apple [b] => banana [c] => cherry [d] => date )
- == (Equality): returns true if both arrays have the same key/value pairs.
$x = array("a" => "apple", "b" => "banana");
$y = array("b" => "banana", "a" => "apple");
if ($x == $y) {
echo "True";
} else {
echo "False";
} // Output: True
- != (Inequality): returns true if both arrays do not have the same key/value pairs.
$x = array("a" => "apple", "b" => "banana");
$y = array("c" => "cherry", "d" => "date");
if ($x != $y) {
echo "True";
} else {
echo "False";
} // Output: True
- === (Identity): returns true if both arrays have the same key/value pairs in the same order and of the same types.
$x = array("a" => "apple", "b=> "banana");
$y = array("a" => "apple", "b" => "banana");
if ($x === $y) {
echo "True";
} else {
echo "False";
} // Output: True
Ternary Operator
The ternary operator is a shorthand way of writing an if-else statement in PHP. The condition is evaluated first, and if it is true, the value_if_true expression is returned. If the condition is false, the value_if_false expression is returned. The ternary operator is particularly useful for writing concise code.
// Basic usage
$is_admin = true;
$message = ($is_admin) ? "Welcome, admin!" : "Access denied";
echo $message; // Output: Welcome, admin!
// Checking if a variable is set
$my_var = null;
$value = $my_var ?? "default value";
echo $value; // Output: default value
Null Coalescing Operator
The null coalescing operator is used to check if a variable is set or not in PHP. If the variable is set, it returns its value. If the variable is not set, it returns the value after the operator. This operator is particularly useful for handling situations where you need to assign a default value to a variable if it is not set.
// Basic usage
$my_var = null;
$value = $my_var ?? "default value";
echo $value; // Output: default value
// Using the null coalescing operator with an array
$array = array("key1" => "value1");
$value = $array["key2"] ?? "default value";
echo $value; // Output: default value
Operator Precedence
Operator precedence is the order in which operators are evaluated in PHP. It is important to understand operator precedence, as it affects the way that expressions are evaluated. Operators with higher precedence are evaluated first.
// Operator precedence
$result = 3 + 4 * 5;
echo $result; // Output: 23
// Using parentheses to override operator precedence
$result = (3 + 4) * 5;
echo $result; // Output: 35
Learn how to use PHP operators with PHP expressions.
Conclusion
Operators in PHP are essential tools that allow developers to perform various operations on data types. There are different types of operators in PHP, such as assignment, comparison, logical, string, array, ternary, and null coalescing operators. Understanding the use of these operators is crucial for writing efficient and readable code.