Search code examples
phpvariable-variables

php get the variable name from other variable


look at this simple script please

$c1 = $_GET[c1];
$c2 = $_GET[c2];
$c3 = $_GET[c3];
$c4 = $_GET[c4];
$c5 = $_GET[c5];
for($i = 1;$i <=5;$i++)
{
    echo $c{$i};//or something else here :/
}

how can i print tha values of variables?

Thanks


Solution

  • You can see on php.net some good examples in the variable page. Read that and take a look at the examples.

    Also, below is your piece of code fixed so it can work:

    <?php
    
    $c1 = $_GET[c1];
    $c2 = $_GET[c2];
    $c3 = $_GET[c3];
    $c4 = $_GET[c4];
    $c5 = $_GET[c5];
    for($i = 1;$i <=5;$i++)
    {
        echo ${"c".$i};
    }