Use PHP's variable variable syntax by assign a $ to a variable whose value is the variable name you want:
<?php
$a = "Hello";
$hello = "Hello Everyone";
echo $$a."<br/>";
?>
Output: Hello Everyone
<?php
$a = "Hello";
$hello = "Hello Everyone";
echo $$a."<br/>";
?>
Output: Hello Everyone
Discussion:
The previous example prints Hello Everyone. Because $a = 'hello', $$a is $hello.
Using curly braces, you can construct more complicated expressions that indicate variable names:
Using curly braces, you can construct more complicated expressions that indicate variable names:
$stooges = array('Moe','Larry','Curly');
$stooge_moe = 'Moses Horwitz';
$stooge_larry = 'Louis Feinberg';
$stooge_curly = 'Jerome Horwitz';
foreach ($stooges as $s) {
print "$s's real name was ${'stooge_'.strtolower($s)}.\n";
}
Moe's real name was Moses Horwitz.
Larry's real name was Louis Feinberg.
Curly's real name was Jerome Horwitz.
$stooge_moe = 'Moses Horwitz';
$stooge_larry = 'Louis Feinberg';
$stooge_curly = 'Jerome Horwitz';
foreach ($stooges as $s) {
print "$s's real name was ${'stooge_'.strtolower($s)}.\n";
}
Moe's real name was Moses Horwitz.
Larry's real name was Louis Feinberg.
Curly's real name was Jerome Horwitz.