PHP Operators
Certainly! PHP operators are used to perform operations on variables and values. Here’s a rundown of the different types of operators in PHP with examples:
1. Arithmetic Operators
These operators perform basic arithmetic operations.
-
Addition (
+
)$a = 5; $b = 10; echo $a + $b; // Output: 15
-
Subtraction (
-
)$a = 10; $b = 3; echo $a - $b; // Output: 7
-
Multiplication (
*
)$a = 4; $b = 5; echo $a * $b; // Output: 20
-
Division (
/
)$a = 20; $b = 4; echo $a / $b; // Output: 5
-
Modulus (
%
) (remainder of division)$a = 10; $b = 3; echo $a % $b; // Output: 1
-
Exponentiation (
**
) (raises a number to the power of another number)$a = 2; $b = 3; echo $a ** $b; // Output: 8
2. Assignment Operators
These operators are used to assign values to variables.
-
Assignment (
=
)$a = 10; echo $a; // Output: 10
-
Addition Assignment (
+=
)$a = 5; $a += 3; // Same as $a = $a + 3; echo $a; // Output: 8
-
Subtraction Assignment (
-=
)$a = 10; $a -= 4; // Same as $a = $a - 4; echo $a; // Output: 6
-
Multiplication Assignment (
*=
)$a = 3; $a *= 4; // Same as $a = $a * 4; echo $a; // Output: 12
-
Division Assignment (
/=
)$a = 20; $a /= 5; // Same as $a = $a / 5; echo $a; // Output: 4
-
Modulus Assignment (
%=
)$a = 10; $a %= 4; // Same as $a = $a % 4; echo $a; // Output: 2
3. Comparison Operators
These operators compare two values and return a boolean result.
-
Equal (
==
)$a = 5; $b = 5; var_dump($a == $b); // Output: bool(true)
-
Identical (
===
) (checks if both value and type are the same)$a = 5; $b = '5'; var_dump($a === $b); // Output: bool(false)
-
Not Equal (
!=
)$a = 5; $b = 6; var_dump($a != $b); // Output: bool(true)
-
Not Identical (
!==
)$a = 5; $b = '5'; var_dump($a !== $b); // Output: bool(true)
-
Greater Than (
>
)$a = 10; $b = 5; var_dump($a > $b); // Output: bool(true)
-
Less Than (
<
)$a = 3; $b = 7; var_dump($a < $b); // Output: bool(true)
-
Greater Than or Equal To (
>=
)$a = 10; $b = 10; var_dump($a >= $b); // Output: bool(true)
-
Less Than or Equal To (
<=
)$a = 5; $b = 8; var_dump($a <= $b); // Output: bool(true)
4. Logical Operators
These operators are used to perform logical operations.
-
And (
&&
)$a = true; $b = false; var_dump($a && $b); // Output: bool(false)
-
Or (
||
)$a = true; $b = false; var_dump($a || $b); // Output: bool(true)
-
Xor (
xor
)$a = true; $b = false; var_dump($a xor $b); // Output: bool(true)
-
Not (
!
)$a = true; var_dump(!$a); // Output: bool(false)
5. String Operators
These operators are used to manipulate strings.
-
Concatenation (
.
)$a = "Hello"; $b = " World"; echo $a . $b; // Output: Hello World
-
Concatenation Assignment (
.=
)$a = "Hello"; $a .= " World"; // Same as $a = $a . " World"; echo $a; // Output: Hello World
6. Array Operators
These operators are used to perform operations on arrays.
-
Union (
+
)$a = array("a" => "apple", "b" => "banana"); $b = array("b" => "berry", "c" => "cherry"); $c = $a + $b; print_r($c); // Output: Array ( [a] => apple [b] => banana [c] => cherry )
-
Equality (
==
)$a = array("a" => "apple", "b" => "banana"); $b = array("a" => "apple", "b" => "banana"); var_dump($a == $b); // Output: bool(true)
-
Identity (
===
)$a = array("a" => "apple", "b" => "banana"); $b = array("a" => "apple", "b" => "banana"); var_dump($a === $b); // Output: bool(true)
-
Inequality (
!=
)$a = array("a" => "apple", "b" => "banana"); $b = array("a" => "apple", "b" => "berry"); var_dump($a != $b); // Output: bool(true)
-
Non-identity (
!==
)$a = array("a" => "apple", "b" => "banana"); $b = array("a" => "apple", "b" => "berry"); var_dump($a !== $b); // Output: bool(true)
7. Increment/Decrement Operators
These operators are used to increment or decrement values.
-
Increment (
++
)$a = 5; $a++; echo $a; // Output: 6
-
Decrement (
--
)$a = 5; $a--; echo $a; // Output: 4
8. Ternary Operator
The ternary operator is a shorthand for an if-else
statement.
- Ternary (
?:
)$a = 10; $b = ($a > 5) ? "Greater than 5" : "5 or less"; echo $b; // Output: Greater than 5
9. Null Coalescing Operator
Used to check if a variable is set and is not null.
- Null Coalescing (
??
)$a = null; $b = $a ?? "Default value"; echo $b; // Output: Default value
Feel free to ask if you need more details on any specific operator or usage!
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Terms Disclaimer About Us Contact Us
Copyright 2023-2025 © All rights reserved.