print variables using a for loop?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wurdup
Forum Commoner
Posts: 39
Joined: Thu Apr 01, 2010 11:36 am

print variables using a for loop?

Post by wurdup »

how do display variables that end in numbers in a for loop?

eg

$product1 = "car";
$product2 = "lorry";

for ($a=1;$a<2;$a++) {

echo $product+$a; // doesn't work

}
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: print variables using a for loop?

Post by Eran »

Code: Select all

$product1 = "car";
$product2 = "lorry";
for ($a=1;$a<=2;$a++) {
    $varname = 'product' . $a;
    echo $$varname; //Variable variable
}
wurdup
Forum Commoner
Posts: 39
Joined: Thu Apr 01, 2010 11:36 am

Re: print variables using a for loop?

Post by wurdup »

cool thx
Post Reply