Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

scope-static

Scope, globals, and static

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Scope, globals, and static: the syntax, APIs, and habits you need before advancing in PHP.

Teams ship Scope, globals, and static on every PHP codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Scope, globals, and static in contexts like: LAMP/LEMP stacks, Laravel apps, WordPress themes/plugins, and shared hosting.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

When you can explain the previous lesson's ideas without copying starter code.

Variables inside a function are local unless imported with global (avoid) or accessed via superglobals. Prefer passing arguments and returning values over mutating globals.

Local scope

function demo(): void {
    $x = 1; // local
}

Static locals

function counter(): int {
    static $count = 0;
    return ++$count;
}

static retains value between calls—useful for cheap memoization or id generators inside one request, not for shared state across requests.

Closures and use

$factor = 2;
$mul = function (int $n) use ($factor): int {
    return $n * $factor;
};

Self-check

  1. Why avoid global $config in functions?
  2. Does a static variable persist across HTTP requests?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Static var use case?
  • Global keyword avoid?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump