Theory
The string that added up
The event portal reads the number of seats from a form. Form data always arrives as a STRING, so $seats holds "350", with quotes. Yet this works:
echo "350" + 10; // prints 360
In Java that would be a type error; in PHP it just... works, because PHP quietly turned the string into a number. This helpful-but-dangerous behaviour is called type juggling, and understanding it (and the == vs === distinction it creates) is the heart of this lesson.
At a glance
PHP data types
| Type | Holds | Example |
|---|---|---|
| string | Text | $e = "Garba Night"; |
| int | Whole numbers | $seats = 350; |
| float | Decimals | $fee = 49.5; |
| bool | true / false | $open = true; |
| array | An ordered/keyed collection | $list = ["Riya", "Aman"]; |
| object | An instance of a class | $user = new User(); |
Theory
Type juggling and casting
PHP is dynamically typed: a variable's type follows whatever value it holds, and can change.
Type juggling is PHP AUTOMATICALLY converting types during an operation: "5" + 3 juggles the string "5" to the number 5, giving 8. Convenient, but it can surprise you.
Type casting is doing it EXPLICITLY: (int)"5abc" gives 5 (it reads leading digits), (int)"abc" gives 0, (float)"3.5" gives 3.5.
Because form input is always strings, juggling is why arithmetic on form data often just works, and casting is how you make the conversion deliberate and safe.
Practical
Operators and the == vs === trap (predict each)
<?php
echo 10 % 3; // 1 (modulus: remainder)
echo 2 ** 3; // 8 (exponent)
echo "5" + 3; // 8 (juggling: string to number)
$msg = "Seats: ";
$msg .= 350; // .= appends -> "Seats: 350"
echo $msg;
var_dump("5" == 5); // bool(true) loose: juggles, then compares
var_dump("5" === 5); // bool(false) strict: different types
?>
Theory
== is loose, === is strict
This is the single most important operator idea in PHP:
- `==` (loose equality) juggles types BEFORE comparing, so
"5" == 5is true and even odd cases like0 == ""can surprise you - `===` (identical) compares value AND type with NO juggling, so
"5" === 5is false (string vs int)
Because juggling can produce unexpected trues, the professional habit is to prefer `===` when you want a reliable comparison. The same applies to != (loose) versus !== (strict). When comparing a form's string to an expected value, === protects you from PHP's helpfulness backfiring.
Quiz
What do var_dump("5" == 5); and var_dump("5" === 5); print, respectively?
- true and true
- true and false: == juggles the string to a number (equal), === also checks type (string vs int, not identical)
- false and false
- false and true
Show the answer
true and false: == juggles the string to a number (equal), === also checks type (string vs int, not identical)
== is LOOSE: it juggles "5" to the number 5, so "5" == 5 is true. === is STRICT: it compares value AND type, and a string is not an int, so "5" === 5 is false. This is THE PHP comparison distinction, and prefering === avoids the surprises loose juggling can cause. Option A forgets === checks type. Option C forgets == juggles. Option D reverses both. Remember: == asks 'equal after juggling?', === asks 'same value and same type?'.
Think first
Why does form validation prefer ===?
The portal checks if a submitted role equals the string "admin". Why might using == here be risky, and === safer? Reason about juggling, then tap.
Show the answer
With ==, PHP juggles types before comparing, which can produce unexpected trues in edge cases (for example, comparisons involving 0 or empty values behave surprisingly under loose rules). For a security-sensitive check like 'is this user an admin?', an unexpected true is a disaster. === compares value AND type with no juggling, so it only returns true when the input is EXACTLY the string "admin": no surprises. The rule, especially for validation and authorization: use === (and !==) so PHP's automatic type conversion cannot backfire into a wrong match. Loose equality is a convenience you switch off where correctness matters.
Watch out
Type and operator traps
== surprises: loose equality juggles types; prefer === for reliable comparisons.
.= vs +=: .= appends to a STRING; += adds NUMBERS: mixing them up corrupts data.
Casting non-numeric strings: (int)"abc" is 0, not an error: validate before trusting.
Assuming form data is numeric: $_GET/$_POST values are STRINGS; cast or validate before math.
/ always gives a result: 10 / 3 is a float 3.333...; use intdiv() or % for whole-number needs.
Theory
Types in hand, now logic
You can now store and compute with every kind of data the portal needs, and you understand the juggling that makes PHP both convenient and occasionally tricky. Next the portal needs to make DECISIONS and REPEAT (if/switch, loops) and, crucially, to hold LISTS: the registration list is an ARRAY. Control structures and arrays, with PHP's powerful array functions, come next.
Summary
Key takeaways
- PHP types: string, int, float, bool, array, object (and null); dynamically typed (type follows value).
- Type juggling auto-converts types in operations: "5" + 3 = 8; type casting ((int)"5abc" = 5) is explicit.
- Operators: arithmetic (+ - / % *), assignment (=, +=, .= appends strings), comparison, logical (&& || !).
- == is loose (juggles types, so "5" == 5 is true); === is strict (value AND type, so "5" === 5 is false).
- Prefer === (and !==) for reliable comparisons, especially in validation and authorization.
- Form data arrives as strings: cast or validate before doing math.
- Memory hook: == juggles, === does not.