PHP Operators
PHP operators are special symbols or keywords that are used to perform operations on variables and values. They are essential for manipulating data and controlling the flow of your PHP programs. Here’s a comprehensive overview of the different types of operators in PHP:
1. Arithmetic Operators
These operators are used to perform arithmetic operations:
+ (Addition): Adds two values.
- (Subtraction): Subtracts one value from another.
* (Multiplication): Multiplies two values.
/ (Division): Divides one value by another.
% (Modulus): Returns the remainder of a division.
** (Exponentiation): Raises one value to the power of another (introduced in PHP 5.6).
$a = 10;
$b = 5;
echo $a + $b; // 15
echo $a - $b; // 5
echo $a * $b; // 50
echo $a / $b; // 2
echo $a % $b; // 0
echo $a ** 2; // 100
2. Assignment Operators
These operators are used to assign values to variables:
= (Assignment): Assigns a value to a variable.
+= (Addition Assignment): Adds and assigns.
-= (Subtraction Assignment): Subtracts and assigns.
*= (Multiplication Assignment): Multiplies and assigns.
/= (Division Assignment): Divides and assigns.
%= (Modulus Assignment): Computes modulus and assigns.
$x = 10;
$x += 5; // Equivalent to $x = $x + 5;
echo $x; // 15
3. Comparison Operators
These operators are used to compare two values:
== (Equal): Checks if two values are equal.
=== (Identical): Checks if two values are equal and of the same type.
!= (Not Equal): Checks if two values are not equal.
!== (Not Identical): Checks if two values are not equal or not of the same type.
> (Greater Than): Checks if the left value is greater than the right value.
< (Less Than): Checks if the left value is less than the right value.
>= (Greater Than or Equal To): Checks if the left value is greater than or equal to the right value.
<= (Less Than or Equal To): Checks if the left value is less than or equal to the right value.
<=> (Spaceship): Performs a three-way comparison (introduced in PHP 7).
$a = 10;
$b = 20;
var_dump($a == $b); // false
var_dump($a === $b); // false
var_dump($a < $b); // true
var_dump($a <= $b); // true
var_dump($a <=> $b); // -1 (less than)
4. Increment/Decrement Operators
These operators are used to increase or decrease a value by one:
++$a (Pre-increment): Increases the value of $a by one before using it.
$a++ (Post-increment): Increases the value of $a by one after using it.
--$a (Pre-decrement): Decreases the value of $a by one before using it.
$a-- (Post-decrement): Decreases the value of $a by one after using it.
$a = 10;
echo ++$a; // 11
echo $a++; // 11 (then $a becomes 12)
echo $a; // 12
5. Logical Operators
These operators are used to perform logical operations:
&& (Logical AND): Returns true if both operands are true.
|| (Logical OR): Returns true if at least one operand is true.
! (Logical NOT): Returns true if the operand is false.
xor (Logical XOR): Returns true if only one operand is true.
$a = true;
$b = false;
var_dump($a && $b); // false
var_dump($a || $b); // true
var_dump(!$a); // false
var_dump($a xor $b); // true
6. String Operators
These operators are used to manipulate strings:
. (Concatenation): Joins two strings.
.= (Concatenation Assignment): Appends one string to another.
$str1 = "Hello";
$str2 = "World";
echo $str1 . " " . $str2; // Hello World
$str1 .= $str2;
echo $str1; // HelloWorld
7. Array Operators
These operators are used to work with arrays:
+ (Union): Combines two arrays.
== (Equality): Checks if two arrays have the same key/value pairs.
=== (Identity): Checks if two arrays have the same key/value pairs and the same order.
!= (Inequality): Checks if two arrays do not have the same key/value pairs.
!== (Non-identity): Checks if two arrays do not have the same key/value pairs or the same order.
php
$array1 = array("a" => "apple", "b" => "banana");
$array2 = array("a" => "apple", "b" => "banana");
var_dump($array1 == $array2); // true
var_dump($array1 === $array2); // true
$array3 = array("a" => "apple");
var_dump($array1 + $array3); // Union: array("a" => "apple", "b" => "banana")
8. Conditional (Ternary) Operator
This is a shorthand for the if-else statement:
? : (Ternary): Returns one of two values based on a condition.
php
$age = 20;
$status = ($age >= 18) ? "Adult" : "Minor";
echo $status; // Adult
9. Null Coalescing Operator
Introduced in PHP 7, this operator checks if a value is set and is not null:
?? (Null Coalescing): Returns the left-hand operand if it exists and is not null; otherwise, returns the right-hand operand.
php
$username = $_GET['user'] ?? 'guest';
echo $username; // Outputs 'guest' if 'user' is not set or is null
10. Execution Operator
This operator executes a command via the shell and returns the output:
` (Backticks): Executes the command inside and returns the result.
php
$output = `ls -l`;
echo $output;
These operators form the core of data manipulation and logic in PHP. Understanding how to use them effectively is key to writing efficient and effective PHP code.