Lesson 2 • Beginner
Variables & Data Types 📦
Master PHP's dollar-sign variables, data types, type juggling, constants, and superglobals.
What You'll Learn in This Lesson
- • Declaring variables with the $ prefix and naming conventions
- • PHP data types: strings, integers, floats, booleans, NULL
- • String interpolation with double quotes vs single quotes
- • Type juggling (automatic) vs explicit type casting
- • Constants with define() and const, plus superglobals
php -S localhost:8000.$name) tells you what's inside, and you can swap the contents anytime. The $ sign is like a sticker that says "this is a variable" — every box must have one.1️⃣ PHP Data Types
| Type | Example | Check Function |
|---|---|---|
| String | "Hello World" | is_string() |
| Integer | 42, -10, 0 | is_int() |
| Float | 3.14, -0.5 | is_float() |
| Boolean | true, false | is_bool() |
| NULL | null | is_null() |
| Array | [1, 2, 3] | is_array() |
Try It: PHP Variables
Declare variables, check types, and see string interpolation in action
// PHP Variables & Data Types (simulated in JavaScript)
// In PHP, every variable starts with $
console.log("=== PHP Variables ===");
console.log();
// String
let name = "Alice Johnson"; // PHP: $name = "Alice Johnson";
console.log("String: $name = " + JSON.stringify(name));
// Integer
let age = 28; // PHP: $age = 28;
console.log("Integer: $age = " + age);
// Float
let price = 49.99; // PHP: $price = 49.99;
console.log("Float: $price = " + price);
// Bo
...2️⃣ Type Juggling & Casting
PHP automatically converts types when mixing them (type juggling). You can also force a conversion with explicit casting:
// Type juggling (automatic) $result = "5" + 3; // 8 (string "5" becomes int) $result = true + 1; // 2 (true becomes 1) // Explicit casting $x = (int)"42.7"; // 42 (truncates decimal) $y = (string)100; // "100" $z = (bool)""; // false (empty string = false)
"hello" + 5 = 5 — non-numeric strings become 0!Try It: Type Casting & Constants
Explore automatic type juggling, explicit casting, constants, and superglobals
// PHP Type Casting & Type Juggling
console.log("=== Type Juggling (Automatic Conversion) ===");
console.log();
console.log("PHP automatically converts types when needed:");
console.log();
// PHP type juggling examples
let strNum = "42";
let num = 8;
console.log('"42" + 8 = ' + (parseInt(strNum) + num));
console.log(" → PHP converts the string '42' to integer 42, then adds");
console.log();
console.log('"hello" + 5 = ' + (0 + 5));
console.log(" → 'hello' becomes 0 (no number found), so 0 + 5
...⚠️ Common Mistakes
name = "Alice" is a constant, not a variable. You need $name."Hello $name" interpolates, 'Hello $name' outputs the literal text.0 == "hello" is true (type juggling!), use === for strict comparison.=== (strict equality) in PHP to avoid type juggling surprises.📋 Quick Reference — Variables & Types
| Syntax | Example | Notes |
|---|---|---|
| $var = value | $age = 25; | Declare variable |
| gettype() | gettype($age) | Returns "integer" |
| (int) | (int)"42" | Cast to integer |
| define() | define('PI', 3.14); | Define constant |
| === | $a === $b | Strict equality |
🎉 Lesson Complete!
You've mastered PHP variables and data types! Next, learn about operators and expressions.
Sign up for free to track which lessons you've completed and get learning reminders.