Python Variables, Strict Types & Dynamic Casting
Variables in PHP start with a dollar sign ($) and are dynamically typed by default. Learn strict type enforcement (declare(strict_types=1)), scalar types (int, float, string, bool), compound types (array, object), and explicit type casting.
"A variable without strict types is like a flexible cardboard box that can hold shoes, books, or water bottles at any time. Adding strict types turns the box into a molded holder that only accepts one precise shape."
Deep Dive: How It Works
Variable Syntax: Always prefixed with $ (e.g. $userId = 101;).
Scalar Types: int, float, string, bool, and special types null and resource.
Explicit Casting: Prefix expressions with (int), (float), (string), (bool), or (array).
Strict Types: declare(strict_types=1); enables strict argument and return type checking across function calls.
Syntax Blueprint
<?php $name = "Alice"; $age = (int)"28"; $isActive = true;
Declare variables with $, assign literals, and convert types explicitly with (type) casting.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Assuming "0" evaluates to true in boolean checks.Why it happens: In PHP, both "" (empty string) and "0" (string containing zero) evaluate to false in boolean contexts.
How to fix: Use strict comparison ($val === "") or strlen($val) > 0 when checking string input.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Cast and Format User Metrics
Declare a variable $rawPrice = "49.99".
Cast $rawPrice to float and store it in $price.
Multiply $price by 2 and print "Total: $" followed by the result.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.