Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction to PHP  ›  Lesson

Variables and Scope

Variables and Scope 14 minA PHP function sees only its own parameters and local variables, so values from the top level of a script must be passed in, while a static local keeps its value between calls.

Variables and Scope

Pageturn keeps a shop-wide discount in a variable at the top of the file. You write a function to work out a sale price, use the discount inside it, and get a warning plus the wrong answer. Nothing is misspelled. The function simply cannot see the variable, and that is PHP's scope rule at work.

The rule

A function sees only its own variables — pass in what it needs.

Variables start with $, then a letter or underscore: $title, $in_stock, $_tmp. They need no declaration. Assigning a value creates the variable, and it can hold any type later.

<?php
$discount = 0.10;           // global scope: the top level of the file

function salePrice($price) {
    return $price - $price * $discount;   // Warning: Undefined variable $discount
}

function salePriceFixed($price, $discount) {
    return $price - $price * $discount;   // uses the parameter
}

echo salePriceFixed(9.99, $discount);   // 8.991

PHP has three scopes you meet early:

  • Global: variables made at the top level of a script.
  • Local: variables made inside a function, including its parameters. They vanish when the function returns.
  • Static: a local variable marked static keeps its value between calls.
function nextOrderNumber() {
    static $n = 0;   // set to 0 only the first time
    $n++;
    return $n;
}
echo nextOrderNumber();   // 1
echo nextOrderNumber();   // 2

Unlike many languages, PHP does not let functions read globals automatically. You can reach in with global $discount; or $GLOBALS['discount'], but that hides a dependency. Passing a parameter says clearly what the function uses.

The shortcut

For every variable inside a function, trace it to where it was born: a parameter, an assignment inside this function, or a static line. If it was born outside, the function does not have it.

Tracing $discount in salePriceStep 1 of 4
  1. Find the variable

    Inside salePrice, the code reads $discount.

Drills

Answer fast — trust the rule.

quick drill

Which is a valid PHP variable name?

quick drill

A function reads $discount, which was set only at the top level of the file. What happens?

quick drill

What does the third call to nextOrderNumber() return?

quick drill

What is the clearest way to give a function access to a value from outside it?

Remember

  • A function sees only its own variables: pass in what it needs.
  • Shortcut: trace each variable to its birth — parameter, local assignment or static.
  • Trap: expecting globals to be visible inside functions, as they are in JavaScript or Python.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhy is the discount ignored?

A function salePrice($price) uses $discount, which was set to 0.10 at the top of the file, but it always returns the full price. Explain why, and describe the cleanest fix.