Variables
Variables are how a script holds onto a value to use later. PHP variables are refreshingly simple to declare — no separate type declaration step required — but understanding exactly what's happening underneath that simplicity, especially around type juggling, avoids a class of subtle bug that trips up many PHP beginners.
Declaring Variables
Every PHP variable name starts with a dollar sign ($) — that's not optional punctuation, it's a required part of the variable's name. No separate "declare this as a string/integer" step exists; PHP figures out the type automatically from the assigned value.
$name and $Name are two genuinely different variables — a frequent source of confusing "undefined variable" warnings when a typo accidentally changes the case partway through a script.
The Core PHP Data Types
Checking a Variable's Type
var_dump() is genuinely one of the most-used debugging tools in everyday PHP — it shows both the type and the exact value of whatever's passed to it, which becomes especially useful once type juggling (below) makes a value's actual type less obvious from reading the code alone.
Type Juggling — PHP's Automatic Type Conversion
PHP is "loosely typed" — it automatically converts between types as needed in many contexts, rather than requiring an explicit conversion every time. This is convenient, and also the source of some of PHP's most commonly mocked quirks.
== ("loose equality") allows type juggling before comparing — 0 == "abc" historically even evaluated to true in older PHP versions, a famously confusing quirk. === ("strict equality") requires both the value AND the type to match exactly, with zero juggling involved. As a strong default habit, prefer === unless you have a specific, deliberate reason to allow type juggling in a comparison.
Explicit Type Casting — When You Want Control
(int)3.7 gives 3, not 4 — a genuinely easy assumption to get wrong if you expect mathematical rounding. Use round() explicitly first if rounding (rather than truncation) is actually what's needed.
Constants
A constant, once defined, cannot be reassigned later in the script — genuinely useful for values that should never change, like a site name or a fixed configuration value.
Coding Challenges
Create three variables: $firstName (string), $age (int), and $heightInMeters (float). Use var_dump() on each one to confirm PHP correctly identified each type, then echo a sentence combining all three using string interpolation.
Predict, then verify with var_dump, the result of each: "10" + 5, "10" . 5, "3.5" + "1.5", and (int)"7 apples". Write a one-sentence explanation for each result.
Write code comparing the integer 1 and the string "1" using both == and ===, echoing a clear message explaining what each comparison returned and why they differ.
Chapter 2 Quick Reference
- $variableName — the $ prefix is required; no separate type declaration needed
- Variable names are case-sensitive — $name and $Name are different variables
- Core types: string, int, float, bool, array, null
- gettype() / var_dump() — check a variable's actual type; var_dump shows type and value together
- Type juggling — PHP automatically converts types as needed in many contexts (arithmetic, comparisons)
- == (loose) vs === (strict) — prefer === as a default habit to avoid juggling surprises
- (int)/(float)/(string) — explicit casting; casting float to int truncates, doesn't round
- define('NAME', value) — constants, no $ prefix, value never changes once set
- Next chapter: operators and control structures — if/else, switch