1

I'm wondering if it is possible to loop a variable within a variable? Here is something I want to setup:

$var1 = Benjamin
$var2 = George
$var3 = Abraham

and probably echo out something like

<li>Benjamin</li>
<li>George</li>
<li>Abraham</li>

but I want to know, if I want to add $var4 = ..., $var5 = ..., is there a way I can do this all in a loop? I'm thinking having an empty() function that'll loop the variable names/numbers until reaches the first empty variable?

1
  • im confused, but i think the answer is an array() Commented Mar 23, 2011 at 0:17

6 Answers 6

7

You could store them in an array.

$names = array('Mike', 'Jim', 'Tom', 'Stacy');

foreach($names as $name){
  echo $name;
}

As seen here: http://www.ideone.com/f7Ce7

Sign up to request clarification or add additional context in comments.

Comments

6

In PHP you can do this:

    $var1 = "foo";
    $var2 = "bar";

    $name = "var1";
    $i=1;
    while( !is_null( $$name ) ) {
            echo '<li>' . $$name . '</li>';
            $i++;
            $name = "var$i";
    }

but a better solution may be using an array and a foreach

6 Comments

Bonus for answering the question asked and caveating it.
wow as the others have said, thanks for creating this. It helps me learn more about how to use php!
@Zydeco: Variable variables are not to be +1'd.
@Tomalak: If you feel that way, then don't, but be considerate of the fact that not everyone thinks like you do. Variable variables are extremely useful and not many people utilize them at all.
|
2

This sounds like you want to use arrays and foreach. Am I missing something?

$presidents = array(
  'Benjamin', 'George', 'Abraham'
);

foreach($presidents as $pres) {
 echo "$pres\n";
}

Comments

2
$var=array('Benjamin', 'George', 'Abraham');

foreach ($var as $name){
echo $name;
}

Comments

0

A better solution would be arrays. define it as: $names = array(0=>"Benjamin",1=>"George",2=>"Abraham");

Then loop through it with:

foreach ($names as $id=>$name)
{
    echo $name;
}

Then reference a name with $names[0], if you want to add another use $names[] = 'William'; Look up more information at:http://php.net/manual/en/language.types.array.php

Comments

0

This solution doesn't require the use of an array.

$var1 = 'Benjamin';
$var2 = 'George';
$var3 = 'Abraham';
//add as many variables as you want

$i = 0;
$currentVariable = 'var'.$i;
while (isset($$currentVariable)) {
    //process variable
    echo $$currentVariable;
    $i++;
}

2 Comments

It doesn't require an array, but an array is the best solution. This is what arrays were created for, back in 1940s. However, you do get credit for answering the literal question correctly. :)
Agreed, Tomalak. An array is the best solution the majority of the time. Variable variables are useful in a handful of cases though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.