Construct a variable

Is there a way to contruct a variable, if I have these stored variables


$var_1 = 3;
$var_2 = 5;
$var_3 = 3;
$variable = 'var_';
$number = 1;

and I need to have a function that runs
while (something){
do something
while ($var_1 > 0){ // this is what I am trying to construct
do something
}
}

where you see the comment I want to make it so the code runs the first time and it uses $var_1 the second time it uses $var_2 etc…

I was trying to construct a variable like this


$var_1 = 3;
$var_2 = 5;
$var_3 = 3;
$variable = 'var_';
$number = 1;
$currentvar = $variable . $number;

and I need to have a function that runs
while (something){
do something
while ($var_1 > 0){ // this is what I am trying to construct
do something
}
}

and then put increments to the $number variable but how can I amke it so I can use it as a variable instead of a string? is this possible?

I just found it here it is for anyone that comes looking for the same

$constructedvar = ${$some_var_1 . $some_var_2};

or you could just use an [FPHP]array[/FPHP].

1 Like

An array would not work for me on this situation because of the way the information is stored in the database.

1 Like

I’m sure you can turn whatever you have in the DB into an array.

Are you using something like this to get the results from the database?

<?php
$results_array=array();
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    $results_array[]=$row;
}
?>

Is the data stored in different tables in the db?

Hint: If you’re retrieving it from a database, it already -is- an array.