PHP variable variables with $$ and curly braces

PHP lets you use the value of one variable as another variable’s name with $$name or ${$name}. PHP calls this a variable variable. If you need to group values under numbered names, an array keeps them in one place.

Use a string as a variable name

When $label contains 'status', $$label refers to $status. Save the example as variable-name.php and run php variable-name.php from a terminal with PHP installed. Save each later example in its own PHP file and run it in the same way.

<?php
$label = 'status';
$$label = 'ready';

echo $status, PHP_EOL;
echo ${$label}, PHP_EOL;

Both lines print ready. The braces in ${$label} mark the expression that supplies the variable name. Assigning through a name that already exists replaces its value. See the PHP manual on variable variables for the syntax.

Leave the dollar sign out of the name string and match the variable’s letter case. To put the resolved value in a double-quoted string, use "Selected: {$label}" or concatenate the value.

Create numbered variables in a loop

The indexes that foreach receives from this array start at zero. Add one and concatenate the result with 'person_' to make each variable name.

<?php
$names = ['Ada', 'Lin', 'Sam'];

foreach ($names as $index => $name) {
    ${'person_' . ($index + 1)} = $name;
}

echo $person_1, PHP_EOL;
echo $person_3, PHP_EOL;

The loop creates $person_1, $person_2, and $person_3. The example prints Ada and Sam. Inside a function, these variables belong to that function’s local scope. Return an array if the caller needs the values. See the PHP manual on variable scope.

Resolving a name at runtime still follows the same scope rules. In a function or method this syntax cannot select a superglobal array, and PHP also disallows dynamic references to $this.

<?php
$status = 'outside';

function localStatus(): string
{
    $label = 'status';
    ${$label} = 'inside';
    return $status;
}

echo localStatus(), PHP_EOL;
echo $status, PHP_EOL;

Save the function example below as scope.php. It prints inside followed by outside. Selecting the same name inside the function leaves the outer variable untouched.

Place array indexes inside or outside the braces

${$lookup[0]} uses the string at $lookup[0] as the variable name. ${$target}[0] resolves the variable name first, then reads the first element of that array.

<?php
$lookup = ['status'];
$status = 'ready';
$target = 'colors';
$colors = ['blue', 'green'];

echo ${$lookup[0]}, PHP_EOL;
echo ${$target}[0], PHP_EOL;

The first line prints ready, the value of $status. The second prints blue, the value of $colors[0]. Compare where the index sits relative to the closing brace.

Store numbered values in an array

Use the numbers as array keys if you want to retrieve the values by number starting at 1. Assigning to $people[$index + 1] keeps every value in $people.

<?php
$names = ['Ada', 'Lin', 'Sam'];
$people = [];

foreach ($names as $index => $name) {
    $people[$index + 1] = $name;
}

echo $people[1], PHP_EOL;
echo $people[3] ?? '(missing)', PHP_EOL;
echo $people[4] ?? '(missing)', PHP_EOL;

The first two lines print Ada and Sam. The third prints (missing) because key 4 is absent. The ?? operator returns its right operand when the key is missing or its value is null. Use array_key_exists() to distinguish a missing key from one whose value is null. See the PHP manual on arrays.

Variable variables can be useful when existing code requires choosing among predefined variable names at runtime. To group values or work with names supplied in a request, define the keys you need and look up values in an array.

For example, accept only expected settings keys such as theme and language, and check the type and allowed values. Using request keys directly as variable names could overwrite existing local variables.

<?php
$input = ['theme' => 'dark', 'language' => 'xx', 'debug' => 'on'];
$settings = ['theme' => 'light', 'language' => 'en'];
$allowed = ['theme' => ['light', 'dark'], 'language' => ['en', 'ko']];

foreach ($allowed as $key => $options) {
    $value = $input[$key] ?? null;
    if (in_array($value, $options, true)) {
        $settings[$key] = $value;
    }
}

echo json_encode($settings), PHP_EOL;

In settings.php below, the allowed theme changes to dark, the unsupported language xx falls back to en, and debug is absent because it is outside the allowlist. Replace 'dark' in the input with ['dark'] and run it again. The strict comparison rejects the array and keeps the default light.