PHP Operators
PHP operators are symbols used to perform operations on variables and values. They are essential for arithmetic calculations, logical comparisons, string manipulations, and more. Understanding PHP operators is crucial for efficient coding and optimizing performance. In this guide, we will explore different types of PHP operators, their functionalities, and practical examples with outputs.
Introduction to PHP Operators
Operators in PHP are categorized based on their functionality. They help in performing mathematical operations, assigning values, comparing data, and controlling program flow. Mastering these operators enhances code efficiency and readability.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical calculations like 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 |
Quotient of $a and $b |
% |
Modulus | $a % $b |
Remainder of $a divided by $b |
** |
Exponentiation | $a ** $b |
$a raised to the power of $b |
Example:
<?php
$a = 10;
$b = 3;
echo "Addition: " . ($a + $b) . "\n"; // Output: 13
echo "Subtraction: " . ($a - $b) . "\n"; // Output: 7
echo "Multiplication: " . ($a * $b) . "\n"; // Output: 30
echo "Division: " . ($a / $b) . "\n"; // Output: 3.333...
echo "Modulus: " . ($a % $b) . "\n"; // Output: 1
echo "Exponentiation: " . ($a ** $b) . "\n"; // Output: 1000
?>
Output
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333
Modulus: 1
Exponentiation: 1000
2. Assignment Operators
Assignment operators assign values to variables. They can also perform operations before assignment.
Operator | Example | Equivalent To |
---|---|---|
= |
$a = $b |
$a = $b |
+= |
$a += $b |
$a = $a + $b |
-= |
$a -= $b |
$a = $a - $b |
*= |
$a *= $b |
$a = $a * $b |
/= |
$a /= $b |
$a = $a / $b |
%= |
$a %= $b |
$a = $a % $b |
Examples:
<?php
$x = 5;
$x += 3; // $x = $x + 3
echo $x; // Output: 8
?>
Output
3. Comparison Operators
Comparison operators compare two values and return a boolean result (true or false).
Operator | Name | Example | Result |
---|---|---|---|
== |
Equal | $a == $b |
true if $a equals $b |
=== |
Identical | $a === $b |
true if $a equals $b and same type |
!= |
Not Equal | $a != $b |
true if $a not equal to $b |
<> |
Not Equal | $a <> $b |
Same as != |
!== |
Not Identical | $a !== $b |
true if $a not equal or not same type |
< |
Less Than | $a < $b |
true if $a less than $b |
> |
Greater Than | $a > $b |
true if $a greater than $b |
<= |
Less Than or Equal | $a <= $b |
true if $a ≤ $b |
>= |
Greater Than or Equal | $a >= $b |
true if $a ≥ $b |
Example:
<?php
$a = 5;
$b = "5";
var_dump($a == $b); // Loose comparison (value only): bool(true)
var_dump($a === $b); // Strict comparison (value and type): bool(false)
var_dump($a != $b); // Loose not equal: bool(false)
var_dump($a !== $b); // Strict not equal: bool(true)
?>
Output
bool(false)
bool(false)
bool(true)
4. Increment/Decrement Operators
These operators increase or decrease a variable's value.
Operator | Name | Example | Effect |
---|---|---|---|
++$a |
Pre-increment | ++$a |
Increment $a by 1, then return $a |
$a++ |
Post-increment | $a++ |
Return $a, then increment by 1 |
--$a |
Pre-decrement | --$a |
Decrement $a by 1, then return $a |
$a-- |
Post-decrement | $a-- |
Return $a, then decrement by 1 |
Example:
<?php
$x = 5;
echo "Pre-increment: " . ++$x . "\n"; // Increments then returns: 6
echo "Post-increment: " . $x++ . "\n"; // Returns then increments: 6 (but $x becomes 7)
echo "Final value: " . $x; // Shows final value: 7
?>
Output
Post-increment: 6
Final value: 7
5. Logical Operators
Logical operators combine conditional statements.
Operator | Name | Example | Result |
---|---|---|---|
&& or and |
AND | $a && $b |
true if both $a and $b are true |
|| or or |
OR | $a || $b |
true if either $a or $b is true |
! |
NOT | !$a |
true if $a is false |
xor |
XOR | $a xor $b |
true if either $a or $b is true, but not both |
Example:
<?php
$a = true;
$b = false;
var_dump($a && $b); // Logical AND: bool(false)
var_dump($a || $b); // Logical OR: bool(true)
var_dump(!$a); // Logical NOT: bool(false)
?>
Output
bool(true)
bool(false)
6. String Operators
PHP has two string operators:
- Concatenation (
.
) - Concatenation assignment (
.=
)
Example
<?php
$str1 = "Hello";
$str2 = "World";
// String concatenation
echo $str1 . " " . $str2 . "\n"; // Output: Hello World
// Compound concatenation
$str1 .= $str2;
echo $str1; // Output: HelloWorld
?>
Output
HelloWorld
7. Array Operators
Array operators compare and combine arrays.
Operator | Name | Example | Result |
---|---|---|---|
+ |
Union | $a + $b |
Union of $a and $b |
== |
Equality | $a == $b |
true if $a and $b have same key/value pairs |
=== |
Identity | $a === $b |
true if $a and $b have same key/value pairs in same order and types |
Example:
<?php
$a = ["a" => "apple", "b" => "banana"];
$b = ["b" => "blueberry", "c" => "cherry"];
print_r($a + $b); // Output: Array ( [a] => apple [b] => banana [c] => cherry )
?>
Output
8. Conditional (Ternary) Operator
The ternary operator (?:
) is a shorthand for if-else.
Example
<?php
$age = 20;
echo ($age >= 18) ? "Adult" : "Minor"; // Output: Adult
?>
Output
9. Null Coalescing Operator
The null coalescing operator (??
) returns the first
non-null value.
Example
<?php
$name = $_GET['name'] ?? 'Guest';
echo $name; // Output: Guest if 'name' is not set
?>
Output
10. Spaceship Operator (<=>)
The spaceship operator compares two values and returns:
- 0 if equal
- 1 if left is greater
- -1 if right is greater
Example
<?php
echo 5 <=> 3; // Output: 1
echo 3 <=> 5; // Output: -1
echo 5 <=> 5; // Output: 0
?>
Output
-1
0
11. Bitwise Operators
Bitwise operators perform operations on bits.
Operator | Name | Example |
---|---|---|
& |
AND | $a & $b |
| |
OR | $a | $b |
^ |
XOR | $a ^ $b |
~ |
NOT | ~$a |
<< |
Left Shift | $a << $b |
>> |
Right Shift | $a >> $b |
Example:
<?php
$a = 5; // 0101
$b = 3; // 0011
echo $a & $b; // Output: 1 (0001)
?>
Output
12. Execution Operator (`)
Executes shell commands.
Example:
<?php
$output = `ls -la`;
echo "<pre>$output</pre>";
?>
13. Type Operator (instanceof)
Checks if an object is an instance of a class.
Example:
<?php
class MyClass {}
$obj = new MyClass();
var_dump($obj instanceof MyClass); // Output: bool(true)
?>
Output
14. Operator Precedence
Operator precedence determines the order of operations.
Precedence | Operators |
---|---|
Highest |
++ , -- , ~ ,
(int) , (string)
|
* , / , % |
|
+ , - , . |
|
< , <= , > ,
>=
|
|
== , != , === ,
!==
|
|
Lowest |
&& , || , ?: ,
=
|
Example:
<?php
echo 2 + 3 * 4; // Output: 14 (Multiplication first)
?>