A PHP variable is a name that starts with $, and you create it by assigning a value: $name = "Ada";. There is no declaration keyword and no type to state, because the value decides the type. You can assign a value of a different type later.
Variable naming rules
A variable name starts with $, then a letter or underscore, then any letters, digits or underscores. Non-ASCII letters count as letters, so $名前 is valid, though ASCII names are the norm. Names are case-sensitive.
| Name | Valid? |
|---|---|
$total, $userName, $user_name | yes |
$_count, $item2 | yes |
$2items | no, cannot start with a digit |
$user-name | no, that is $user minus name |
$this | no, reserved for the current object; assigning it is a fatal error |
So $city, $City and $CITY are three different variables. Use snake_case ($first_name) or camelCase ($firstName), and stay with one style per project. The PSR coding standards leave variable naming open on purpose; Laravel and Symfony code mostly uses camelCase.
Use variables in strings
Inside double quotes PHP replaces variables with their values; inside single quotes it does not. Wrap the variable in braces when letters follow it directly or when you access an array element by a string key:
The strings page covers quotes, concatenation and interpolation in detail.
Undefined variables and default values
Reading a variable that was never assigned gives null, and your PHP prints a warning:
Warning: Undefined variable $discount in /home/index.php on line 3
Check first with isset, or use the null coalescing operator ?? to supply a default. Both run without a warning:
$x ??= 0; is the short form of $x = $x ?? 0;. Form and URL values are the usual case: $page = $_GET['page'] ?? 1;.
Variable scope in functions
Variables defined outside a function are not visible inside it, and variables created inside a function disappear when it returns. This is the most common surprise for people coming from JavaScript:
Without the ?? 0, the first function would warn about an undefined $taxRate. Passing values as parameters keeps functions predictable and easy to test, and it is what you should reach for first. The functions page covers parameters and return values.
The global keyword and $GLOBALS
When a function really must use a global variable, declare it with global, or go through the $GLOBALS array, which holds every global variable by name:
Globals make code harder to follow, because any function can change them. They appear in older code and WordPress plugins (global $wpdb;); in new code, pass values in or use a class.
Static variables keep their value
A static variable inside a function is created once and keeps its value between calls. It is still local: code outside the function cannot see it.
Variable variables ($$name)
$$name uses the value of $name as the name of another variable. It works, but arrays almost always do the same job more clearly:
<?php
$field = "color";
$$field = "blue"; // creates $color
echo $color, "\n";
// The clearer way to have names chosen at runtime:
$settings = [];
$settings[$field] = "blue";
echo $settings["color"], "\n";
Copy or reference
Assigning one variable to another copies the value, arrays included. Put & in front to make both names point at the same value instead:
Objects behave differently: assigning an object copies a handle to the same object, so changes through either variable are visible through both. Use clone for a real copy.
Common mistake: a variable set inside an if or a loop
PHP has no block scope, so a variable created inside if, for or foreach still exists after it. That helps, but it hides a bug: when the block does not run, the variable was never created.
Without the default line, an empty $orders would leave $message undefined and print a warning. Give every variable a value before the block that might set it.
Frequently Asked Questions
How do you declare a variable in PHP?
Assign a value to a name that starts with $: $count = 10;. PHP has no var or let keyword and no type in the declaration; the variable is created by the first assignment and takes the type of its value.
Why can't my function see a variable defined outside it?
PHP functions have their own local scope and do not see global variables automatically. Pass the value as a parameter (the clean option), declare global $name; inside the function, or read $GLOBALS['name']. Closures can capture it with use ($name), and arrow functions fn() capture it automatically.
What is a static variable in PHP?
A local variable declared with static inside a function, like static $calls = 0;. It is initialized once and keeps its value between calls to that function, which is useful for counters and simple caches.
Are PHP variable names case-sensitive?
Yes. $name, $Name and $NAME are three different variables. Function and class names, on the other hand, are case-insensitive in PHP.
How do I delete a variable in PHP?
Call unset($name);. Afterwards isset($name) is false and reading it gives an "Undefined variable" warning. Assigning null keeps the variable but empties it.