PHP Variables
PHP Variables: Naming Rules, Types, and Scope
A PHP variable is a named container that holds data such as text, numbers, arrays, or objects in memory. Every PHP variable starts with a dollar sign $ followed by an identifier name, and PHP assigns its type dynamically when you store a value.
PHP Variable Naming Rules
Follow these four rules whenever you name a variable in PHP:
- Start with a letter or underscore:
$userAgeand$_cacheKeyare valid, whereas$2ndPlacecauses a syntax error. - Use only letters, numbers, and underscores: Avoid spaces, hyphens, or symbols like
$user-name. - Keep casing consistent: Variable names are case-sensitive, so
$orderIdand$orderidare distinct. - Never reassign
$this:$thisis reserved inside classes to point to the active object instance.
<?php
declare(strict_types=1);
$customerName = "Nadia Karim";
$itemCount = 3;
$unitPrice = 14.50;
$totalCost = $itemCount * $unitPrice;
echo "{$customerName} ordered {$itemCount} items for $" . number_format($totalCost, 2) . PHP_EOL;
Variable Scope: Local, Global, and Static
Where you declare a variable controls which parts of your script can read or change it. Variables created inside a function stay local to that function and disappear once it finishes.
<?php
$siteCurrency = "USD"; // Global scope
function formatAmount(float $price): string
{
global $siteCurrency; // Pull global variable into function scope
static $callCount = 0; // Preserved across function calls
$callCount++;
return "[#{$callCount}] {$siteCurrency} " . number_format($price, 2);
}
echo formatAmount(25.0) . PHP_EOL;
echo formatAmount(40.5) . PHP_EOL;
Passing values into functions as parameters is much cleaner than relying on the global keyword, because it keeps dependencies explicit and easy to test.
Common Mistakes to Avoid
- Using undefined variables: Reading
$discountbefore assigning it triggers a warning in PHP 8 and defaults tonull. - Overusing
global: Mutating global variables inside functions creates hidden side effects that make debugging painful. - Accidental assignment inside
if: Writingif ($isAdmin = true)assignstrueinstead of checking equality with===.
Frequently Asked Questions
Do I need to declare a variable type in PHP?
No. PHP infers the type from the assigned value automatically, though you can enforce strict types on function parameters, return values, and class properties.
How do I check if a variable exists and is not null?
Use isset($var) to confirm a variable is declared and non-null, or use the null coalescing operator $name = $input ?? 'Guest'.
What does a static variable inside a function do?
A static local variable keeps its value between multiple calls to that same function instead of resetting on every call.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.
Test Your Knowledge
You've completed this section! Take a quick 5-question quiz to check your understanding.