PHP access variable dynamically

Access variable value dynamically using variable variable. A variable variable takes the value of a variable and treats that as the name of a variable. We can run time access any variable dynamically by $$(variable variable).

Simple example for print variable value dynamically

<?php
$name_1 = 'this is name 1';
$name_2 = 'this is name 2';
$name_3 = 'this is name 3';

for($i = 1; $i < 4; $i++)
{
    $varName = 'name_'.$i;
    echo $$varName;
    echo ${$varName};
    echo '<br/>';
}
?>

Simple example for access class method dynamically

<?php
class Test
{
    public function print_1()
    {
        echo 'print_1';
    }

    public function print_2()
    {
        echo 'print_2';
    }

    public function print_3()
    {
        echo 'print_3';
    }
}

$testObj = new Test;

for($i = 1; $i < 4; $i++)
{
    $methodName = 'print_'.$i;
    $testObj->$methodName();
    $testObj->{$methodName}();
}
?>

For more instruction and example you can visit php manual here.