Courses/PHP/Variables & Data Types

    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

    1️⃣ PHP Data Types

    TypeExampleCheck Function
    String"Hello World"is_string()
    Integer42, -10, 0is_int()
    Float3.14, -0.5is_float()
    Booleantrue, falseis_bool()
    NULLnullis_null()
    Array[1, 2, 3]is_array()

    Try It: PHP Variables

    Declare variables, check types, and see string interpolation in action

    Try it Yourself »
    JavaScript
    // 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)
    ⚠️
    Watch out: "hello" + 5 = 5 — non-numeric strings become 0!

    Try It: Type Casting & Constants

    Explore automatic type juggling, explicit casting, constants, and superglobals

    Try it Yourself »
    JavaScript
    // 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

    ⚠️
    Forgetting the $name = "Alice" is a constant, not a variable. You need $name.
    ⚠️
    Single vs double quotes"Hello $name" interpolates, 'Hello $name' outputs the literal text.
    ⚠️
    Comparing with == vs ===0 == "hello" is true (type juggling!), use === for strict comparison.
    💡
    Pro Tip: Always use === (strict equality) in PHP to avoid type juggling surprises.

    📋 Quick Reference — Variables & Types

    SyntaxExampleNotes
    $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 === $bStrict 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.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service