Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 2 of 9
Variables, Data Types & Type Juggling
PHP is a dynamically-typed language - variables don't have fixed types and can change type at runtime. PHP 8.x introduces strict type declarations that enforce type safety - critical for production code. Understanding both behaviours is essential for writing reliable PHP.
Variables & All Data Types
<?php
declare(strict_types=1); // ALWAYS add this - enforces type safety at function boundaries
// Variables start with $ - no declaration keyword needed
$name = "Alice"; // string
$age = 28; // int
$price = 49.99; // float (double)
$active = true; // bool
$nothing = null; // null
// Type inspection
var_dump($age); // int(28)
var_dump($price); // float(49.99)
gettype($name); // "string"
is_string($name); // true
is_int($price); // false - it's a float
// ── Strings ────────────────────────────────────────────────────
$first = "John";
$last = 'Doe';
// Double quotes: interpolation works
echo "Hello, $first $last!"; // Hello, John Doe!
echo "Hello, {$first}!"; // curly brace syntax - clearer
// Single quotes: NO interpolation - faster for static strings
echo 'Hello, $first!'; // Hello, $first! (literal)
// Heredoc - multi-line with interpolation
$html = <<<HTML
<h1>Welcome, {$first}</h1>
<p>Your age is {$age}</p>
HTML;
// Nowdoc - multi-line, NO interpolation (like single quotes)
$raw = <<<'TEXT'
No $interpolation here.
TEXT;
// ── Arrays ─────────────────────────────────────────────────────
$tags = ["php", "web", "backend"]; // indexed array
$user = ["name" => "Alice", "age" => 28]; // associative array
$matrix = [[1,2], [3,4]]; // multidimensional
echo $tags[0]; // php
echo $user["name"]; // Alice
// ── Type Juggling - PHP's implicit conversion (can be dangerous!) ──
var_dump(0 == "foo"); // true in PHP 7 (!! changed in PHP 8)
var_dump(0 == "foo"); // false in PHP 8 (string not automatically cast to 0)
var_dump(0 === "foo"); // false (strict - ALWAYS use ===)
// NULL coalescing
$username = $_GET["user"] ?? "guest"; // "guest" if not set or null
$count = $data["count"] ?? 0;
// Nullsafe operator (PHP 8.0+)
$city = $user?->getAddress()?->getCity(); // no error if any is nullType Declarations (PHP 8.x)
<?php
declare(strict_types=1);
// Typed function parameters and return type
function formatPrice(float $price, int $decimals = 2): string {
return '$' . number_format($price, $decimals);
}
echo formatPrice(49.9); // $49.90
// formatPrice("fifty"); // TypeError in strict mode - great!
// Union types (PHP 8.0)
function processId(int|string $id): string {
return "ID: " . $id;
}
// Nullable types
function findUser(?int $id): ?array {
if ($id === null) return null;
return ["id" => $id, "name" => "Alice"];
}
// readonly properties (PHP 8.1)
class Post {
public function __construct(
public readonly int $id,
public readonly string $title,
) {}
}
$post = new Post(1, "Hello PHP");
// $post->title = "Change"; // Error! readonlyQuick Quiz
Tip
Tip
Practice Variables Data Types Type Juggling in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Variables, Data Types & Type Juggling
Practice Task
Note
Practice Task - (1) Write a working example of Variables Data Types Type Juggling from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Variables Data Types Type Juggling is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?