PHP — Variables
📌 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.
$userNameor$_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$Colorare three distinct variables.
2PHP Variable Scopes (Local, Global, Static & Superglobals)
| Variable Scope | Technical Rule & Memory Behavior |
|---|---|
| Local Scope | Variables declared inside a function exist ONLY inside that function execution frame. Destroyed upon return. |
| Global Scope | Variables declared outside functions. Accessible inside functions ONLY via global $var or $GLOBALS['var']. |
| Static Scope | Declared with static $count = 0; inside a function. Value is preserved across multiple function calls! |
| Superglobals | Built-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 variableNotice in PHP 8+. Always check withisset($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
$_GETor$_POST.
💻 Live PHP Code Execution
Test and run this PHP script in our online web compiler environment:
Open in Online PHP Compiler →