Operators
By the end of this lesson you'll be able to do maths, join text, and compare values in PHP — and you'll know the one comparison rule ( === over == ) that prevents a whole category of bugs.
Part of the free Php course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ Arithmetic Operators
An operator is a symbol that acts on values — the values it works on are called its operands . The arithmetic ones do exactly what you'd expect from a calculator: + , - , * , and / . Two are worth a closer look: % ( modulus ) gives the remainder after dividing — great for "is this number even?" — and ** raises a number to a power. Note that / can return a float (a decimal) even when both inputs are whole numbers.
2️⃣ Joining Text: . and .=
This is the one that trips up newcomers from other languages: PHP joins strings with the dot ( . ), not the plus sign. Writing "5" + "3" tries to add them as numbers (giving 8 ), whereas "5" . "3" glues them into "53" . The .= operator is the text version of += — it appends to a string you already have.
3️⃣ Assignment & Increment
A single = is assignment — it stores the value on the right into the variable on the left (it is not "equals"; that's == , coming next). The compound operators ( += -= *= /= %= ) do a calculation and reassign in one step, so $x += 5 just means $x = $x + 5 . To bump a value by exactly 1, use ++ or -- — and watch the position : $n++ uses the value then adds, while ++$n adds then uses.
4️⃣ Comparison: == vs === and the Spaceship
Comparison operators ask a yes/no question and hand back a boolean ( true or false ). The most important rule in this whole lesson lives here. == is loose equality: it converts types first, so 5 == "5" is true . === is strict equality: the value and the type must match, so 5 === "5" is false . Prefer === almost always — loose comparison ("type juggling") is a famous source of bugs. The other operators are != (not equal), < , > , < = , > = , and the spaceship < = > , which returns -1 , 0 , or 1 for a three-way compare (ideal for sorting).
Now you try. The script below is almost complete — fill in each ___ using the 👉 hint, then run it and check it against the Output panel.
5️⃣ Logical Operators
Logical operators combine true/false values so you can ask compound questions. && ( AND ) is true only when both sides are true; || ( OR ) is true when either side is true; ! ( NOT ) flips a boolean. PHP also has word forms and / or — they look friendlier but bind looser than = , which causes a classic surprise shown below. The advice is simple: use && and || .
6️⃣ Null Coalescing & Ternary
These operators let you pick a value in one line. ?? ( null coalescing ) means "use the left value, but if it's null or unset, use the right" — perfect for defaults, and it won't warn on undefined variables. ??= assigns the fallback only when the variable is currently null. The ternary condition ? a : b is a compact if/else: it returns a when the condition is true, otherwise b . The short ternary ?: returns the left value if it's truthy. The key difference: ?? only reacts to null , while ?: reacts to any falsy value ( "" , 0 , false ).
One more guided exercise. Pick the operator each comment asks for, then run it and compare with the Output panel.
7️⃣ Operator Precedence
Precedence is the order operators run in — exactly like school maths, where * happens before + . So 2 + 3 * 4 is 14 , not 20 . Comparisons run before && and || , which is usually what you want. When in any doubt, add parentheses ( ) — they force your order and make intent obvious to the next reader (often future you).
Common Errors (and the fix)
- A comparison with == behaves weirdly — you hit type juggling . With loose == , PHP converts types first, so "5" == 5 is true and other surprises lurk. The fix is almost always to use strict === , which also checks the type.
- Your if is always true — you wrote if ($x = 5) with one = . A single = assigns 5 to $x (and the result is truthy), so the branch always runs. Comparison needs == or === .
- "5" + "3" gave 8 , not "53" — in PHP + is maths only, so it converts numeric strings and adds them. To join text, use the dot: "5" . "3" .
- Comparing a number to a string of text behaves unexpectedly — mixing types in a comparison is risky. If a value came from a form it's a string ; convert it first with (int)$value (or (float) ) before comparing, then use === .
- $total = $a + $b * 2 isn't what you expected — that's precedence : * runs before + . Wrap the part you want first in parentheses: ($a + $b) * 2 .
Pro Tips
- 💡 Default to === . Reach for loose == only when you deliberately want type conversion — which is rare.
- 💡 Use ?? for missing data, ?: for "empty-ish" data. ?? reacts only to null ; ?: also catches "" and 0 .
- 💡 Stick to && / || , not the word forms — their looser precedence causes the $x = true and false trap.
- 💡 Parentheses are free. When precedence is unclear, add them; readable beats clever.
📋 Quick Reference — PHP Operators
Operator
Meaning
Example → Result
+ - * / % **
Arithmetic
15 % 4 → 3
. .=
String join / append
"Hi " . $name
= += -= *= /=
Assign / update
$x += 5
++ --
Increment / decrement
$n++ → adds 1
== ===
Loose / strict equal
5 === "5" → false
!= < > < = > =
Not-equal / ordering
3 < = 3 → true
< = >
Spaceship (3-way)
1 < = > 5 → -1
&& || !
Logical and / or / not
$a && $b
?? ??=
Null coalescing
$x ?? "default"
? : ?:
Ternary / short ternary
$age > = 18 ? "a" : "b"
Frequently Asked Questions
Mini-Challenge: Shopping Receipt
No code is filled in this time — just a brief and an outline. Write it yourself, run it on onecompiler.com/php or your own machine, then check your result against the expected output in the comments. This is exactly the write-run-check loop you'll use on every real script.
🎉 Lesson Complete!
- ✅ Arithmetic uses + - * / % ** ; % is the remainder and / can return a float
- ✅ Join text with the dot . (and append with .= ) — never +
- ✅ += -= ++ -- update a variable in place; ++$n vs $n++ differ by position
- ✅ Prefer === over == — strict equality checks the type too and avoids type-juggling bugs
- ✅ < = > compares three ways; && || ! combine conditions
- ✅ ?? / ??= give defaults; the ternary ?: is a one-line if/else; parentheses settle precedence
- ✅ Next lesson: Control Flow — use these comparisons inside if / else and switch to make decisions
Practice quiz
What does the modulus operator % return?
- The quotient of a division
- A power
- The remainder after dividing
- A percentage
Answer: The remainder after dividing. % gives the remainder after dividing, so 15 % 4 is 3.
What is the result of 2 + 3 * 4 in PHP?
- 14
- 20
- 24
- 9
Answer: 14. Multiplication has higher precedence than addition, so 3 * 4 happens first: 2 + 12 = 14.
Which operator joins two strings together in PHP?
- +
- &
- ||
- .
Answer: .. PHP uses the dot (.) for concatenation; + is arithmetic only.
What does the expression "5" + "3" evaluate to in PHP?
- The string "53"
- The number 8
- An error
- The string "5 3"
Answer: The number 8. + is maths only, so PHP converts the numeric strings and adds them, giving 8.
What does the spaceship operator return for 1 <=> 5?
- -1
- 0
- 1
- true
Answer: -1. <=> returns -1 when the left operand is smaller than the right.
Which comparison checks BOTH value and type?
- ==
- <=>
- ===
- !=
Answer: ===. === is strict equality: the values must match in value and type.
What is the value of $n after: $n = 5; echo $n++;
- $n stays 5; it prints 6
- echo prints 5, then $n becomes 6
- echo prints 6, then $n becomes 5
- $n becomes 4
Answer: echo prints 5, then $n becomes 6. Post-increment $n++ uses the value (5) first, then adds 1, so $n becomes 6.
What does $name = $username ?? "Guest" do?
- Always sets $name to "Guest"
- Compares the two values
- Concatenates them
- Uses $username, but "Guest" if $username is null/unset
Answer: Uses $username, but "Guest" if $username is null/unset. ?? is null coalescing: it falls back to the right side only when the left is null or unset.
Why is && preferred over the word 'and' in conditions?
- 'and' is invalid PHP
- 'and' binds looser than = and causes surprises
- 'and' is much slower
- 'and' only works on numbers
Answer: 'and' binds looser than = and causes surprises. The word 'and' binds looser than =, so $x = true and false sets $x to true unexpectedly.
What does var_export(0 == "a") output in modern PHP 8?
- true
- 0
- false
- An error
Answer: false. In PHP 8 a non-numeric string is not juggled to 0, so 0 == "a" is false.
Continue this course
- Previous: Variables & Data Types
- Next: Control Flow