PHP Basics: Variables, Types, and Type Juggling Explained

PHP’s type system is famously loose, which is either a feature or a footgun depending on who you ask. Understanding how variables and type juggling actually work saves you from a whole category of “wait, why is this true” bugs.

Variable Basics

PHP variables are dynamically typed — no declaration needed, just assign:

$name = "Alice";      // string
$age = 30;             // int
$price = 19.99;        // float
$active = true;        // bool
$data = null;           // null

var_dump($age + $price); // float(49.99)

Type Juggling Gotchas

PHP’s loose comparison (==) coerces types before comparing, which causes classic surprises like "0" == false being true. Always prefer strict comparison (===) unless you specifically want coercion.