In PHP, a single dollar sign introduces a normal variable, such as $name. A double dollar sign is not a separate variable type; it creates a variable variable, meaning PHP uses the value of one variable as the name of another variable.
Variable variables are powerful but can make code harder to read and validate. Arrays or associative maps are often clearer.
At a glance
| Point | $ (normal variable) | $$ (variable variable) |
|---|---|---|
| Example | $name = 'Ada'; | $key = 'name'; $$key refers to $name |
| Readability | Straightforward | Can be confusing in complex code |
| Dynamic data | Use arrays/objects in most application code | Can dynamically reference local variables |
| Security concern | Normal validation rules apply | Dangerous if untrusted input controls variable names |
| Typical recommendation | Preferred | Use sparingly; arrays/maps are often clearer |
$ (normal variable)
The standard PHP variable syntax: $name, $count, $userId.
$$ (variable variable)
An indirect variable reference. PHP first evaluates the variable after the first $, then uses that value as another variable name.
Step-by-step example
Consider:
$fruit = 'apple';
$apple = 'green';
echo $$fruit;
PHP reads $fruit first and gets the string apple. It then evaluates $apple, producing green.
Curly braces remove ambiguity
With arrays, object properties or complex expressions, PHP parsing can become ambiguous. Explicit curly-brace syntax can clarify what is being dereferenced, but the exact syntax depends on the PHP version and expression.
When dynamic names become complicated, that is often a sign an associative array would be easier to maintain.
Why arrays are usually better
Instead of creating $apple, $pear, $orange dynamically, use $colors['apple'], $colors['pear'] and $colors['orange']. The data structure is explicit, iterable and easier to validate or pass between functions.
Frequently asked questions
Is $$ the same as a reference?
No. PHP references use &; variable variables are dynamic name lookup.
Can $$ use user input?
Technically yes, but allowing untrusted input to control variable names can create unsafe and unpredictable code.
Does PHP require $ before every variable?
Yes for ordinary variable syntax.
When is $$ useful?
Occasionally in metaprogramming or legacy dynamic code, but most modern application code is clearer with arrays/objects.
Sources and further reading
KnowDifferences Editorial Team
Independent explanations with definitions, practical examples and references. Read our editorial approach.