Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A static variable in PHP is a locally scoped variable that retains its state across multiple invocations of the function or method in which it is defined. Unlike standard local variables, which are allocated on the call stack and destroyed when the function returns, a static variable is initialized only once and persists in memory for the duration of the script’s execution. Syntax and Declaration Static variables are declared using the static keyword followed by a variable declaration with an initializer.
function incrementCounter() {
    static $counter = 0; // Declaration with initializer, evaluated only on the first invocation
    $counter++;
    return $counter;
}
Technical Characteristics
  • Lexical Scope: The variable remains strictly bound to the local scope of the function. It cannot be accessed, modified, or referenced from the global scope or by other functions.
  • Initialization Phase: The variable declaration with an initializer is evaluated exactly once. Prior to PHP 8.1, static variables could only be initialized with constant expressions (e.g., scalar values or arrays of scalars). PHP 8.1 introduced support for new expressions in initializers. As of PHP 8.3, static variables can be initialized using arbitrary dynamic expressions, including function calls.
  • Memory Lifecycle: The variable resides in a static memory segment rather than the function’s execution context (stack frame). When the function execution terminates, the static variable bypasses standard garbage collection.
Object-Oriented Behavior The behavior of static variables within object-oriented structures depends on the context in which they are defined:
  • Class Instances: When a static variable is defined inside a non-static class method, its state is shared across all instances of that class. The variable is bound to the method’s execution context in the compiled bytecode, not to the individual object instance ($this).
  • Class Inheritance: As of PHP 8.1, static variables in inherited methods are shared between the parent and child classes. Prior to PHP 8.1, inherited methods did not share the static variable and maintained separate, independent states.
  • Traits: If a trait method contains a static variable, every class that uses that trait receives its own independent copy of the static variable.
Execution Comparison
// Standard Local Variable
function standardVar() {
    $a = 0;
    echo ++$a;
}
standardVar(); // Outputs: 1
standardVar(); // Outputs: 1 (Stack frame recreated, variable re-initialized)

// Static Variable
function staticVar() {
    static $a = 0;
    echo ++$a;
}
staticVar(); // Outputs: 1
staticVar(); // Outputs: 2 (Stack frame recreated, but static state retained)
Static Variables vs. Static Properties A static variable declared inside a method is distinct from a static property declared on a class.
  • A static class property (public static $var) is bound to the class itself, accessed via the scope resolution operator (ClassName::$var), and its accessibility is dictated by visibility modifiers (public, protected, private).
  • A static variable (static $var) declared inside a method remains strictly local to that specific method’s block scope, completely invisible to the rest of the class.
Master PHP with Deep Grasping Methodology!Learn More