Introduction
In the previous tutorial, you learned about PHP variables, constants, and data types. Now you are ready to use those values to perform calculations, compare information, and build logic into your programs.
PHP provides different types of operators for working with data. Operators allow you to perform mathematical calculations, assign values, compare values, combine conditions, and manipulate strings.
For example, you can calculate the total price of products:
$total = $price * $quantity;
You can also check whether a user is logged in:
$isLoggedIn == true
Operators are used throughout almost every PHP application, so understanding them is essential before learning conditional statements and loops.
What Is an Operator?
An operator is a symbol or keyword that tells PHP to perform a specific operation.
For example:
$a + $b
Here, + is the operator.
If:
$a = 10;
$b = 5;
then:
echo $a + $b;
produces:
15
The values being operated on are called operands.
1. Arithmetic Operators
Arithmetic operators are used for mathematical calculations.
The main arithmetic operators are:
| Operator | Meaning |
|---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus |
** |
Exponentiation |
Addition
$a = 10;
$b = 5;
echo $a + $b;
Output:
15
Subtraction
echo $a - $b;
Output:
5
Multiplication
echo $a * $b;
Output:
50
Division
echo $a / $b;
Output:
2
Modulus
The % operator returns the remainder after division.
echo 10 % 3;
Output:
1
Modulus is useful for checking whether a number is even or odd.
2. Assignment Operators
Assignment operators are used to assign values to variables.
The basic assignment operator is:
=
Example:
$name = "John";
You can also combine assignment with arithmetic.
$number = 10;
$number += 5;
echo $number;
Output:
15
Other assignment operators include:
+=
-=
*=
/=
%=
For example:
$price = 100;
$price -= 20;
echo $price;
Output:
80
These operators provide a shorter way to update existing values.
3. Comparison Operators
Comparison operators are used to compare two values.
They are especially important when creating conditions.
Common comparison operators include:
| Operator | Meaning |
|---|---|
== |
Equal value |
=== |
Equal value and type |
!= |
Not equal |
!== |
Not equal value or type |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal |
<= |
Less than or equal |
For example:
$age = 20;
var_dump($age > 18);
Output:
bool(true)
The expression asks whether $age is greater than 18.
== vs ===
This is an important concept in PHP.
The == operator compares values after type conversion when applicable.
var_dump(10 == "10");
This can return:
bool(true)
The === operator checks both the value and the data type.
var_dump(10 === "10");
This returns:
bool(false)
because 10 is an integer while "10" is a string.
When you want strict comparison, === is generally the safer choice.
4. Logical Operators
Logical operators allow you to combine multiple conditions.
The most common logical operators are:
| Operator | Meaning |
|---|---|
&& |
AND |
| ` | |
! |
NOT |
AND
Both conditions must be true.
$age = 25;
$isStudent = true;
var_dump($age > 18 && $isStudent);
Both conditions are true, so the result is:
bool(true)
OR
At least one condition must be true.
$age = 16;
$isStudent = true;
var_dump($age >= 18 || $isStudent);
Because $isStudent is true, the overall result is true.
NOT
The ! operator reverses a Boolean value.
$isLoggedIn = false;
var_dump(!$isLoggedIn);
The result is:
bool(true)
Logical operators become extremely useful when creating login systems, access restrictions, search filters, and validation rules.
5. Increment and Decrement Operators
Increment operators increase a value by one.
$count = 5;
$count++;
echo $count;
Output:
6
The decrement operator reduces a value by one:
$count = 5;
$count--;
echo $count;
Output:
4
These operators are frequently used in loops.
You will see them again when we learn PHP loops.
6. String Operators
PHP provides operators for combining strings.
The . operator is used to concatenate strings.
Example:
$firstName = "John";
$lastName = "Smith";
$fullName = $firstName . " " . $lastName;
echo $fullName;
Output:
John Smith
You can also use .= to append text to an existing string.
$message = "Hello";
$message .= " John";
echo $message;
Output:
Hello John
This is useful when dynamically constructing messages or HTML content.
7. Operator Precedence
When an expression contains multiple operators, PHP follows an order to determine which operation happens first.
For example:
$result = 10 + 5 * 2;
echo $result;
The multiplication happens first.
So the calculation becomes:
10 + 10 = 20
Therefore:
20
If you want to control the order, use parentheses:
$result = (10 + 5) * 2;
echo $result;
Now the result is:
30
Using parentheses makes complex expressions easier to understand and reduces mistakes.
Practical Example: Calculate a Product Total
Let's use operators to create a simple product calculation.
<?php
$productPrice = 25;
$quantity = 4;
$total = $productPrice * $quantity;
echo "Product Price: $" . $productPrice . "<br>";
echo "Quantity: " . $quantity . "<br>";
echo "Total: $" . $total;
The output will be:
Product Price: $25
Quantity: 4
Total: $100
Here we used:
-
Variables
-
Multiplication
-
String concatenation
-
echo
These are simple concepts, but they form the foundation of more advanced PHP applications.
Practical Example: Check User Eligibility
Operators can also be used to compare information.
<?php
$age = 21;
$isEligible = $age >= 18;
var_dump($isEligible);
The result is:
bool(true)
This type of logic can later be combined with if statements to display different messages.
Common Beginner Mistakes
Using = instead of ==
This:
$age = 18;
assigns a value.
This:
$age == 18
compares a value.
Do not confuse assignment with comparison.
Forgetting the string concatenation operator
Incorrect:
echo "Hello " $name;
Correct:
echo "Hello " . $name;
Confusing == and ===
Remember:
== → compares value
=== → compares value and type
For strict comparisons, use ===.
Practice Task
Create a PHP program with these variables:
$price = 50;
$quantity = 3;
$discount = 10;
Calculate:
-
The original total price
-
The discount amount
-
The final price
Then create another variable:
$age = 20;
Use a comparison operator to check whether the person is at least 18 years old.
Finally, use the . operator to display a complete message containing the calculated result.
Conclusion
Operators allow PHP to perform calculations, compare values, combine conditions, update variables, and manipulate strings.
In this tutorial, you learned:
-
Arithmetic operators
-
Assignment operators
-
Comparison operators
-
Logical operators
-
Increment and decrement operators
-
String operators
-
Operator precedence
These operators are essential for building application logic.
In the next tutorial, we will use these concepts to make decisions with PHP Conditional Statements.
Next Tutorial: PHP Conditional Statements – if, else, elseif, and switch