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 PHP variable is a named identifier that represents a dynamically typed memory location used to store data during script execution. Internally, the Zend Engine represents variables using a C structure known as a zval (Zend Value), which stores the variable’s value, its data type, and reference counting metadata for garbage collection.

Syntax and Naming Conventions

All variables in PHP are prefixed with a dollar sign ($). The identifier immediately following the dollar sign must adhere to strict lexical rules:
  • It must begin with a letter (A-Z, a-z), an underscore (_), or an extended ASCII character (bytes from 128 through 255, such as ä, é, or ñ).
  • Subsequent characters can be letters, numbers (0-9), underscores, or extended ASCII characters.
  • Variable names are strictly case-sensitive ($Data and $data point to different zval containers).
$validVariable = "String";
$_validVariable = 10;
$validVariable2 = true;
$año = 2023; // Valid extended ASCII character

// $2invalidVariable = "Throws a ParseError";
// $invalid-variable = "Throws a ParseError";

Type System and Assignment

PHP utilizes a dynamic, weak typing system by default. Variables do not require explicit type declarations upon initialization. The Zend Engine infers the data type at runtime based on the assigned value, and a single variable can be reassigned to different data types during its lifecycle (type juggling).
$var = 100;        // Inferred as integer
$var = "String";   // Reassigned, now inferred as string
Copy-on-Write (CoW) By default, variables are assigned by value. PHP optimizes memory usage through a Copy-on-Write mechanism. When a variable is assigned to another, both point to the same zval. Memory is only duplicated if one of the variables is subsequently modified. Assignment by Reference Variables can be assigned by reference using the ampersand (&) operator. This binds two identifiers to the exact same zval container, meaning modifications to one affect the other.
$a = 10;
$b = &$a; // $b is a reference to $a
$b = 20;  // $a is now also 20

Variable Scope

Scope dictates the context in which a variable is defined and accessible. PHP implements four primary variable scopes:
  1. Global Scope: Variables declared outside of any function or class. Unlike languages like C, global variables are not automatically accessible inside functions. They must be explicitly imported into the local scope using the global keyword or accessed via the $GLOBALS associative array.
  2. Local Scope: Variables declared within a function. They are allocated on the stack frame of the function call and are destroyed when the function returns.
  3. Static Scope: Variables declared within a function using the static keyword. They are initialized only once and persist their value across multiple invocations of that specific function, without exposing the variable to the global scope.
  4. Superglobals: Built-in associative arrays (e.g., $_GET, $_POST, $_SERVER, $_SESSION, $_ENV) that are automatically available in all scopes. They do not require the global keyword to be accessed within local contexts.
$globalVar = "Accessible globally";

function scopeExample() {
    global $globalVar; // Importing global scope
    
    $localVar = "Destroyed after execution";
    
    static $staticVar = 0; // Persists across calls
    $staticVar++;

    // Superglobals are automatically accessible
    $method = $_SERVER['REQUEST_METHOD'];
}

Variable Variables

PHP supports variable variables, a metaprogramming feature that allows the name of a variable to be evaluated and defined dynamically at runtime. This is achieved by prefixing an existing variable with an additional dollar sign ($$).
$identifier = "dynamicName";
$$identifier = "Evaluated value"; 

// The above creates a variable named $dynamicName
echo $dynamicName; // Outputs: Evaluated value
Master PHP with Deep Grasping Methodology!Learn More