- Declaring Variables in PHP
- PHP Data Types
- PHP Arrays
- Types of PHP Operators
- PHP Strings
- PHP Expressions
- PHP Control Structures
- PHP Functions
- PHP Form Handling – Read Form Inputs & Handle File Uploads
- How to Connect PHP to MySQL Database Using MySQLi
- Executing Simple Queries in PHP
- Handling Results in PHP
- Handling Sessions and Cookies
Types of PHP Operators
PHP offers different types of operators:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional (Ternary) operator
We’ll understand each one with clear explanations and practical examples.
Arithmetic Operators
These operators are used to do basic math.
Common examples:
$x + $y → Adds two numbers
$x – $y → Subtracts $y from $x
$x * $y → Multiplies $x with $y
$x / $y → Divides $x by $y
$x % $y → Gives remainder
$x ** $y → Power (like 2 ** 3 = 8)
Real-world example:
You want to calculate the total bill of two products.
$price1 = 120;
$price2 = 80;
$total = $price1 + $price2; // Total is 200
Assignment Operators
Assignment operators assign values to variables.
Examples:
$x = 10 → Stores 10 in $x
$x += 5 → Adds 5 to $x
$x -= 2 → Subtracts 2 from $x
$x *= 4 → Multiplies $x by 4
$x /= 2 → Divides $x by 2
$x %= 3 → $x becomes remainder of $x ÷ 3
Use case:
Updating scores after a game round:
$score = 50;
$score += 10; // Now score is 60
Comparison Operators
Used when you want to compare two values — like checking if someone passed or failed.
Examples:
$x == $y → True if values are same
$x === $y → Also checks if type is same
$x != $y → True if values are not equal
$x > $y, $x < $y → Greater or less than
$x >= $y, $x <= $y → Greater/less than or equal to
Use case:
Check if a student passed:
$marks = 40;
if ($marks >= 35) {
echo "Pass";
}
Increment and Decrement Operators
Used to increase or decrease a value by 1.
Types:
++$x → Increases first, then uses value
$x++ → Uses value, then increases
–$x → Decreases first, then uses
$x– → Uses value, then decreases
Use case:
Counting visitors on a website:
$visitors = 100;
$visitors++;
Logical Operators
Used to make decisions based on multiple conditions.
Examples:
$x && $y → True if both are true
$x || $y → True if any one is true
!$x → Opposite (true becomes false)
$x xor $y → True if only one is true
Use case:
Allow login if password is correct and user is active:
if ($isActive && $passwordCorrect) {
echo "Login success";
}
String Operators
Used to join or update strings.
Examples:
$x . $y → Joins two strings
$x .= $y → Adds right string to left variable
Use case:
$first = "Good ";
$second = "Morning";
echo $first . $second; // Output: Good Morning
Array Operators
Used to compare arrays or combine them.
Examples:
$a + $b → Union of arrays
$a == $b → True if same values
$a === $b → True if same order and values
$a != $b, $a <> $b → Not equal
$a !== $b → Not identical
Use case:
$fruit1 = array("a" => "apple", "b" => "banana");
$fruit2 = array("b" => "banana", "a" => "apple");
var_dump($fruit1 == $fruit2); // true
var_dump($fruit1 === $fruit2); // false
Conditional (Ternary) Operator
A short way to write if...else
.
Syntax:
(condition) ? value_if_true : value_if_false;
Use case:
$age = 18;
$status = ($age >= 18) ? "Adult" : "Minor"; // Output: Adult
Summary
Operators in PHP are the foundation of logic, math, and control. Whether you’re checking if a user is eligible for a discount or calculating totals in an e-commerce cart, operators are behind the scenes doing the work.