PHP — Variables

🐘 PHP 8.2+ 🟢 Lesson 6 of 52 📂 Phase 03: Variables and Data Types 📅 2026 Edition
📌 Covered in this chapter: Variable ante enti? · $ symbol prefix · Variable declaration · Variable assignment · Reassigning values · Variable naming rules · Case sensitivity · Local, Global, Static scope · Constants define() & const

Welcome to PHP — Variables in our PHP Complete Masterclass! Declare and scope PHP variables using $ prefixes, global/static scopes, and define constant values.

1PHP Variables & $ Symbol Prefix Rules

In PHP, every variable name MUST start with a dollar sign $ followed by a valid variable identifier. PHP variables are dynamically typed, meaning you do not need to specify data types explicitly upon declaration.

⚡ PHP Variable Naming Rules
  • Must start with a letter or underscore (e.g. $userName or $_status). Cannot start with a number!
  • Can only contain alphanumeric characters and underscores (a-z, A-Z, 0-9, _).
  • Variable names are CASE-SENSITIVE! $color, $COLOR, and $Color are three distinct variables.
2PHP Variable Scopes (Local, Global, Static & Superglobals)
Variable ScopeTechnical Rule & Memory Behavior
Local ScopeVariables declared inside a function exist ONLY inside that function execution frame. Destroyed upon return.
Global ScopeVariables declared outside functions. Accessible inside functions ONLY via global $var or $GLOBALS['var'].
Static ScopeDeclared with static $count = 0; inside a function. Value is preserved across multiple function calls!
SuperglobalsBuilt-in global arrays ($_GET, $_POST, $_SESSION, $_SERVER) available everywhere automatically.
Zend Engine Copy-On-Write (COW) Memory Architecture: $a = "Large String Data"; ──> Points to Memory Buffer (refcount = 1) $b = $a; ──> Points to SAME Memory Buffer (refcount = 2) [NO Memory Duplicate!] $b .= " Modified"; ──> Triggers Copy-On-Write! $b gets new memory buffer (refcount = 1)
3Production Code Examples: Scopes & Constants
PHP — Variables, Scopes & Constants ▶ Run Code
<?php
// 1. Global Variable
$globalAppName = "Our Compiler Platform";

// 2. Constants (Compile-time & Runtime)
define("API_VERSION", "v3.0");
const APP_ENV = "production";

function processVisitor() {
    global $globalAppName; // Access global scope
    
    // 3. Static Variable (Preserves memory state across calls)
    static $totalVisits = 0;
    $totalVisits++;
    
    echo "App: " . $globalAppName . " | Visit #" . $totalVisits . "<br>";
}

processVisitor(); // Output: Visit #1
processVisitor(); // Output: Visit #2
processVisitor(); // Output: Visit #3

echo "API Version: " . API_VERSION;
?>
4Common Pitfalls & Security Best Practices
⚠️ Variable Traps in Production
  • Accessing uninitialized variables throws an Undefined variable Notice in PHP 8+. Always check with isset($var) or use Null Coalescing $var ?? 'default'.
  • Avoid overusing global $var. Pass arguments explicitly into functions to keep code testable and decoupled.
  • Never populate global variables directly from un-sanitized $_GET or $_POST.
💻 Live PHP Code Execution

Test and run this PHP script in our online web compiler environment:

Open in Online PHP Compiler →
OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on PHP 8.2+ · Last updated August 2026