Originally published on zero2php.dev — part of the Zero to PHP course. View this lesson on the original site.

The idea

Inside a function, variables you assign are usually local to that function unless you bring outer scope in deliberately (later: use closures; for globals, global—avoid leaning on globals while learning).

Example

<?php

declare(strict_types=1);

$x = 1;

function show(): void
{
    $y = 2;
    echo $y, "\n";
}

show();

What to remember for now

For now: one script with top-level variables is enough. When functions arrive in Chapter 2, scope becomes a daily topic—this lesson is a heads-up, not the full ruleset.

Exercise

Complete show() so it assigns 2 to $y, then echos $y (digits only). Call show() once at the bottom—output must be 2.

From Zero to PHP — PHP foundations: environment, syntax, and values