What is an Operator in PHP?
PHP Operators are symbols or keywords that let you work with variables and values in PHP (Hypertext Preprocessor).
PHP is a common programming language used on websites.
With these operators, coders can change or manipulate data, compare items, do math, and guide the flow of their programs.
Simply put, these operators can be used to apply an action to values or variables.
For Example:
$add= 15+ 70; (15and 70) are operands and (+) is the operator.
An operator in PHP takes (one or more) an expression or any value in programming and passes another value to become an expression itself.
What are the different types of operators in PHP?
There are Different Types of Operators in PHP:
- Comparison operators
- Conditional assignment operators
- Arithmetic operators
- Array operators
- String operators
- Assignment operators
- Increment/Decrement operators
- Logical operators
- Spaceship Operators (Introduced in PHP 7)
Comparison Operators in PHP
Comparison operators in PHP are used to compare two values or expressions and return a boolean result (either true or false) based on the comparison.
These operators are fundamental for making decisions and controlling the flow of execution in PHP programs.
== | Equal to | Returns true if the values of two operands are equal. “$a == $b” |
!=, <> | Not Equal to | Returns true if the values of two operands are not equal. “$a != $b” or “$a <> $b” |
=== | Identical to | Returns true if both the value and the data type of the operands are identical. “$a === $b” |
!== | Not identical to | Returns true if both the value and the data type of the operands are different. “$a !== $b” |
> | Greater than | Returns true if the value of the left operand is greater than the value of the right operand. “$a > $b” |
>= | Greater than or Equal to | Returns true if the value of the left operand is greater than or equal to the value of the right operand. “$a >= $b” |
< | Less than | Returns true if the value of the left operand is less than the value of the right operand. “$a < $b” |
<= | less than or Equal to | Returns true if the value of the left operand is less than or equal to the value of the right operand. “$a <= $b” |
Conditional Assignment Operators in PHP
Conditional assignment operators in PHP are shorthand expressions that are used to assign values to variables based on certain conditions.
It combines conditional statements with assignment operations precisely and efficiently.
?: | Ternary Operator | If the condition evaluates to true, the value of “value_if_true “ is assigned to $variable ; otherwise, the value of “value_if_false “ is assigned. |
?? | Null Coalescing Operator | If $value is set and not null, $variable is assigned the value of $value ; otherwise, it’s assigned the value of $default_value . |
$variable = (condition) ? value_if_true : value_if_false; //Using a Ternary Operator
$variable = $value ?? $default_value; //Using a Null Coalescing Operator
Arithmetic Operators in PHP
Arithmetic operators in PHP are used to perform mathematical operations on numeric values.
These operators allow you to manipulate numbers by adding, subtracting, multiplying, dividing, and more.
Operators | Name | Operation | Syntax |
+ | Addition | adding the two operands together. | $a + $b |
– | Subtraction | Subtracts the second operand from the first. | $a – $b |
* | Multiplication | Multiplies two operands. | $a * $b |
/ | Division | Divides the first operand by the second. | $a / $b |
% | Modulus | Returns the remainder of the division of the first operand by the second. | $a % $b |
** | Exponentiation | Raises the first operand to the power of the second. | $a ** $b |
++ | Increment | Increases the value of a variable by one. | $a++ |
— | Decrement | Decreases the value of a variable by one. | $b– |
Array Operators in PHP
Array Operators in PHP are used to manipulate arrays, which are a fundamental data structure in PHP for storing and organizing data.
These operators provide functionality for combining, comparing, and modifying arrays.
Operator | Name | Operation | Syntax |
+ | Union | Returns the union of two arrays, merging them together without duplicates from the second array. | $p + $k |
== | Equality | Check if the two arrays have the same key/value pairs. | $p == $k |
!= | Inequality | Check if the two arrays do not have the same key/value pairs. | $p != $k |
=== | Identity | Check if the two arrays are identical, meaning they have the same key/value pairs in the same order. | $p === $k |
!== | Non-Identity | Check if the two arrays are not identical. | $p !== $k |
<> | Inequality | Check if the two arrays do not have the same key/value pairs. | $p<>$k |
String Operators in PHP
String operators in PHP are used to manipulate strings, which are sequences of characters.
These operators provide functionality for the concatenation operator and concatenating assignment operator.
Operators | Name | Operation | Syntax |
. | Concatenation | Combines two strings together to create a single string. | $p.$k |
.= | Concatenation and assignment | Appends one string to another and assigns the result to the first string. | $p.=$k |
Assignment Operators in PHP
Assignment operators in PHP are used to assign values to variables.
These operators allow you to store data in variables and manipulate them as needed.
Operator | Name | Operators | Syntax |
= | Assign | Assigns the value on the right-hand side to the variable on the left-hand side. | $a = $b |
+= | Add then Assign | Adds the value of the right operand to the variable and assigns the result to the variable. | $a += $b |
-= | Subtract then Assign | Subtracts the value of the right operand from the variable and assigns the result to the variable. | $a -= $b |
*= | Multiply then Assign | Multiplies the variable by the value of the right operand and assigns the result to the variable. | $a *= $b |
/= | Divide then Assign (quotient) | Divides the variable by the value of the right operand and assigns the result to the variable. | $a /= $b |
%= | Divide then Assign (remainder) | Divides the value of the variable by the value of the right operand and assigns the remainder of the division to the variable. | $a %= $b |
Increment/Decrement Operators in PHP
Increment and decrement operators in PHP are used to increase or decrease the value of a variable by one, respectively.
These operators provide a shorthand way to perform these common operations.
Operator | Name | Operation | Syntax |
++ | Pre-Increment | increments the value of the variable by one, and then returns the incremented value. | ++$pre |
— | Pre-Decrement | decrements the value of the variable by one, and then returns the decremented value. | –$pre |
++ | Post-Increment | returns the current value of the variable, and then increments the value of the variable by one. | $post++ |
— | Post-Decrement | returns the current value of the variable, and then decrements the value of the variable by one. | $post– |
Spaceship Operators in PHP
The spaceship operator (PHP 7) (<=>) in PHP, also known as the three-way comparison operator, is used for comparing two expressions or values as whether the first value is greater than, equal, or less than the 2nd value.
It returns one of three values:
- -1: if the left operand is less than the right operand,
- 0: if the operands are equal, and
- 1: if the left operand is greater than the right operand.
Condition | Syntax | Return Value |
$p < $k | $p <=> $k | -1 ($k is greater) |
$p > $k | $p <=> $k | 1 ($p is greater) |
$p == $k | $p <=> $k | 0 (both are equal) |
How is the Ternary Conditional Operator used in PHP?
The ternary conditional operator in PHP, represented by the syntax “?:”, is a shortened version of the if-else statement.
It is used to evaluate a condition and return one of two values depending on whether the condition is true or false.
The syntax of the ternary operator is:
(condition) ? true_value : false_value;
Here’s how it works:
- The condition is evaluated first.
- If the condition is true, the value after the “?” is returned.
- If the condition is false, the value after the “:” is returned.
$age = 20;
$message = ($age >= 18) ? "You are an adult" : "You are a minor";
echo $message;
- If the value of $age is greater than or equal to 18, the message “You are an adult” is assigned to $message.
- If the value of $age is less than 18, the message “You are a minor” is assigned to $message.
The ternary operator is often used in situations where a simple decision needs to be made based on a condition, and it helps to keep code concise and readable.
Conclusion
This article covers various types of operators in PHP. It provides a comprehensive overview of each type of operator and their functionalities, serving as a valuable resource for PHP developers.
By exploring these operators, readers gain a deeper understanding of how to manipulate data and control program flow effectively in PHP.
For more insightful tutorials and articles on PHP development, readers are encouraged to explore the author’s previous and latest works.