Operators and Expressions in PHP
What are Operators in PHP?
Operators in PHP are symbols that perform operations on variables and values. These can be used to perform mathematical calculations, compare values, assign values, manipulate strings, and evaluate logic.
An expression in PHP is a combination of values, variables, and operators that returns a value.
1. Arithmetic Operators in PHP
These are used to perform basic mathematical operations.
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 |
Example:
<?php
$x = 10;
$y = 3;
echo $x + $y; // Output: 13
echo "<br>";
echo $x % $y; // Output: 1
?>
2. Assignment Operators in PHP
Assignment operators assign values to variables.
Operator | Meaning | Example | Equivalent To |
---|---|---|---|
= | Assign | $x = 5 | 5 |
+= | Add and assign | $x += 3 | $x = $x + 3 |
-= | Subtract and assign | $x -= 2 | $x = $x - 2 |
*= | Multiply and assign | $x *= 2 | $x = $x * 2 |
/= | Divide and assign | $x /= 2 | $x = $x / 2 |
%= | Modulus and assign | $x %= 2 | $x = $x % 2 |
Example:
<?php
$score = 10;
$score += 5;
echo $score; // Output: 15
?>
3. Comparison Operators in PHP
Used to compare two values and return a boolean (true
or false
).
Operator | Description | Example | Returns |
---|---|---|---|
== | Equal | $x == $y | true if equal (ignores type) |
=== | Identical | $x === $y | true if equal and same type |
!= or <> | Not equal | $x != $y | true if not equal |
!== | Not identical | $x !== $y | true if not same type or value |
> | Greater than | $x > $y | true if greater |
< | Less than | $x < $y | true if less |
>= | Greater than or equal | $x >= $y | true if greater or equal |
<= | Less than or equal | $x <= $y | true if less or equal |
Example:
<?php
$a = 10;
$b = "10";
echo ($a == $b) ? "Equal<br>" : "Not Equal<br>"; // Equal
echo ($a === $b) ? "Identical" : "Not Identical"; // Not Identical
?>
4. Logical Operators in PHP
Logical operators are used to combine conditional statements.
Operator | Description | Example | Result |
---|---|---|---|
&& or and | True if both are true | $x && $y | Logical AND |
` | or or` | True if one is true | |
! | Not | !$x | True if $x is false |
Example:
<?php
$isAdmin = true;
$isLoggedIn = false;
if ($isAdmin && $isLoggedIn) {
echo "Welcome, Admin!";
} else {
echo "Access Denied!";
}
?>
5. String Operators in PHP
Used for working with and joining strings.
Operator | Description | Example |
---|---|---|
. | Concatenation | $a . $b joins two strings |
.= | Concatenation assignment | $a .= $b appends right string to left |
Example:
<?php
$first = "Hello ";
$second = "World!";
$full = $first . $second;
echo $full; // Output: Hello World!
?>
6. Operator Precedence in PHP
Operator precedence determines how expressions are evaluated in the absence of parentheses. PHP follows the standard math logic (BODMAS).
Example:
<?php
$result = 5 + 3 * 2;
echo $result; // Output: 11 (multiplication happens before addition)
?>
To force order, use parentheses:
$result = (5 + 3) * 2; // Now result is 16
Common Mistakes with Operators
- Mixing assignment
=
with comparison==
- Forgetting parentheses in logical expressions
- Confusing
==
with===
- Misusing
.=
instead of.
in string joining
Best Practices
- Always use
===
for type-safe comparisons - Wrap complex conditions in parentheses
- Use meaningful variable names in expressions
- Avoid deeply nested conditions when possible
Notes:
- PHP supports a wide variety of operators: arithmetic, assignment, comparison, logical, and string
- Operator precedence affects how expressions are evaluated
- Use
===
for strict equality,!==
for strict inequality - Use
.
to concatenate strings in PHP - Logical operators like
&&
,||
are vital in control structures
Practice Tasks
Task 1: Calculate Final Price
$price = 100;
$discount = 20; // in %
$final = $price - ($price * $discount / 100);
echo "Final Price: $final";
Task 2: Login Checker
$isMember = true;
$isPaid = false;
if ($isMember && $isPaid) {
echo "Access Granted";
} else {
echo "Upgrade Required";
}
Task 3: String Combiner
$first = "PHP ";
$second = "Tutorial";
echo $first . $second; // Output: PHP Tutorial
๐ก Explore More PHP Learning Tools & Resources
PHP Practice Quiz
Test your PHP skills with real coding questions and scoring.
PHP Interview Questions
Prepare for interviews with common PHP questions and answers.
Educational AI Tutor
Ask PHP questions and get instant AI-powered explanations.
Question Generator
Auto-generate PHP interview questions for quick practice.