A variable in PHP stores data such as strings, numbers, arrays, and objects. Variables can be updated during script execution, and PHP determines their data types automatically.
- Declaration: Variable names begin with $, followed by a valid identifier.
- Dynamic Typing: Variables do not require explicit type declarations.
- Case Sensitivity: $name and $Name are different variables.
- Value Assignment: Variables can store and update different values.
Declaring Variables in PHP
PHP variables are created by assigning a value using the = operator.
Syntax:
$variable_name = value;Example:
<?php
$name = "XYZ";
$age = 30;
$salary = 45000.50;
$isEmployed = true;
?>
Variable Naming Rules
- Variable names must start with a letter or underscore (_), not a number.
- Names can contain letters, numbers, and underscores.
- Variable names are case-sensitive.
- Use descriptive names such as $userName and $totalAmount.
- Avoid reserved keywords and names that conflict with PHP syntax.
Valid and Invalid Variable Names
<?php
$firstName = "Alen"; // Valid
$_age = 25; // Valid
// $2ndPlace = "Ben"; // Invalid: starts with a number
$class = "Physics"; // Valid
?>
PHP Variable Scope
Variable scope determines where a variable can be accessed. PHP supports local variables, global variables, static local variables, and superglobals.

1. Local Variables
Variables declared inside a function are local to that function and cannot be accessed directly outside it.
<?php
$num = 60;
function local_var() {
$num = 50;
echo "Inside function: $num\n";
}
local_var();
echo "Outside function: $num";
?>
2. Global Variables
Variables declared outside functions have global scope. Use the global keyword to access them inside a function.
<?php
$num = 20;
function global_var() {
global $num;
echo "Inside function: $num\n";
}
global_var();
echo "Outside function: $num";
?>
3. Static Variables
A static local variable retains its value between function calls instead of being reinitialized each time.
<?php
function static_var() {
static $num = 5;
$sum = 2;
$sum++;
$num++;
echo $num . "\n";
echo $sum . "\n";
}
static_var();
static_var();
?>
Here, $num retains its updated value between calls, while $sum is initialized again on each call.
4. Superglobals
Superglobals are built-in PHP variables accessible from any scope, including inside functions. Common examples include $_GET, $_POST, $_SESSION, $_COOKIE, $_SERVER, and $GLOBALS.
<?php
echo $_SERVER['SERVER_NAME'];
?>
This example displays the server name provided by the server environment.
Variable Variables
Variable variables allow a variable's value to determine the name of another variable. PHP uses $$ syntax for this purpose.
<?php
$a = 'hello';
$$a = 'World';
echo $hello;
?>
Here, $a contains hello, so $$a creates the variable $hello with the value World.
Best Practices for Using Variables
- Use descriptive names such as $totalAmount instead of $x.
- Follow a consistent naming convention, such as camelCase.
- Initialize variables before using them.
- Avoid reserved keywords and confusing variable names.