Table of Contents:
PHP Operators Complete Guide | Arithmetic, Assignment, Comparison & Logical Operators in PHP
PHP Operators – Complete Beginner to Advanced Guide
Welcome to phponline.in, your trusted learning hub for PHP.
This complete PHP Operators guide covers everything — from basic arithmetic to advanced bitwise operations, with syntax, examples, real-world use cases, and performance tips.
What you learn
- Introduction to PHP Operators
- What is an Operator in PHP?
- PHP Arithmetic Operators
- PHP Assignment Operators
- PHP Comparison Operators
- PHP Logical Operators
- PHP Increment & Decrement Operators
- PHP String Operators
- PHP Array Operators
- PHP Type Operators (
instanceof
) - PHP Bitwise Operators
- PHP Ternary and Null Coalescing Operators
- Operator Precedence & Associativity
- Combining Operators in PHP
- Real-World Examples of Operators
- Common Mistakes with Operators
- Best Practices for Using Operators
- Performance Considerations for Operators
1. Introduction to PHP Operators
In PHP, operators are symbols or keywords that perform operations on variables and values.
Example:
$x = 10 + 5; // + is an arithmetic operator
2. What is an Operator in PHP?
An operator tells PHP to perform a specific action such as addition, comparison, assignment, or logical evaluation.
3. PHP Arithmetic Operators
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | $x + $y | Sum of $x and $y |
- | Subtraction | $x - $y | Difference |
* | Multiplication | $x * $y | Product |
/ | Division | $x / $y | Quotient |
% | Modulus | $x % $y | Remainder |
** | Exponentiation | $x ** $y | Power |
Example:
$a = 10;
$b = 3;
echo $a % $b; // Output: 1
4. PHP Assignment Operators
Operator | Example | Equivalent To |
---|---|---|
= | $x = 10 | $x = 10 |
+= | $x += 5 | $x = $x + 5 |
-= | $x -= 5 | $x = $x - 5 |
*= | $x *= 5 | $x = $x * 5 |
/= | $x /= 5 | $x = $x / 5 |
%= | $x %= 5 | $x = $x % 5 |
5. PHP Comparison Operators
Operator | Description | Example |
---|---|---|
== | Equal | $x == $y |
=== | Identical (value & type) | $x === $y |
!= or <> | Not equal | $x != $y |
!== | Not identical | $x !== $y |
> | Greater than | $x > $y |
< | Less than | $x < $y |
>= | Greater or equal | $x >= $y |
<= | Less or equal | $x <= $y |
<=> | Spaceship | $x <=> $y |
Example:
echo 5 <=> 10; // Output: -1
6. PHP Logical Operators
Operator | Description | Example |
---|---|---|
&& | AND | $x && $y |
` | ` | |
! | NOT | !$x |
and | AND (low precedence) | $x and $y |
or | OR (low precedence) | $x or $y |
xor | XOR | $x xor $y |
7. PHP Increment & Decrement Operators
Operator | Example | Description |
---|---|---|
++$x | Pre-increment | Increment before use |
$x++ | Post-increment | Increment after use |
--$x | Pre-decrement | Decrement before use |
$x-- | Post-decrement | Decrement after use |
8. PHP String Operators
Operator | Description | Example |
---|---|---|
. | Concatenation | $a . $b |
.= | Concatenation assignment | $a .= $b |
9. PHP Array Operators
Operator | Description | Example |
---|---|---|
+ | Union | $a + $b |
== | Equality | $a == $b |
=== | Identity | $a === $b |
!= or <> | Inequality | $a != $b |
!== | Non-identity | $a !== $b |
10. PHP Type Operators (instanceof
)
Example:
class Test {}
$obj = new Test();
var_dump($obj instanceof Test); // true
11. PHP Bitwise Operators
Operator | Description |
---|---|
& | AND |
` | ` |
^ | XOR |
~ | NOT |
<< | Shift left |
>> | Shift right |
12. PHP Ternary & Null Coalescing Operators
Ternary:
$result = ($age >= 18) ? "Adult" : "Minor";
Null Coalescing:
$name = $_GET['name'] ?? 'Guest';
13. Operator Precedence & Associativity
PHP follows precedence rules. Example:
echo 5 + 3 * 2; // 11
14. Combining Operators in PHP
You can chain multiple operators:
$total = ($a + $b) * $c / $d;
15. Real-World Examples
- Calculating discount prices
- Checking login credentials
- Validating form inputs
- Working with binary permissions
16. Common Mistakes
- Confusing
=
with==
- Using
and
instead of&&
in complex expressions - Not understanding precedence
17. Best Practices
- Use strict comparison (
===
) - Use parentheses for clarity
- Avoid long chained operations
18. Performance Considerations
- Logical operators short-circuit evaluation
- Bitwise operations are faster for flags
Learn more
Frequently Asked Questions (FAQs)
Q1: What is the difference between ==
and ===
in PHP?
A: ==
compares values, ===
compares values and types.
Q2: Which operator is used for string concatenation?
A: The .
(dot) operator.
Q3: What is the spaceship operator?
A: <=>
returns -1, 0, or 1 for comparisons.
Q4: What is ??
in PHP?
A: Null coalescing operator, returns first non-null value.
Q5: Are bitwise operators used often?
A: Mostly in low-level operations and permission flags.