Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to loop through var1, var2, var3, etc... 1

Status
Not open for further replies.

mhamilton3

Programmer
Oct 31, 2001
129
I have a variable list of paramters that can be passed in to a PHP script. They are var1, var2, var3, etc. I want to use a loop to step through the list and I don't know how to do it.
$i = 0;
do {
print $var$i
if (!$var$i) {
break;
}
}

I want the print statement to print the contents of the variable var1, then the contents of var2, etc. Any help would be greatly appreciated. Thanks
 
Are the vars an array?

for ($i = 0; $i < count ($arr); $i++) {
echo $arr[$i];
} Regards
David Byng
bd_logo.gif

davidbyng@hotmail.com
 
Unfortunately the values are not in an array. They are variables that come from a dynamically created page that has 9 different fields listed in sequential order, first1, last1, email1, phone1, etc. I suppose I could enter each name into an array and step through the array (since I know the names), but that means setting up 9 arrays with 9 entries. Is there and easier way. For example Foxpro has the & and the () which will evaluate the contents of a variable so I could dynamically have myvar = 'first' + '$i' and then I could use &myvar or (myvar) to walk through the values.
 
Thank you very much sleipnir214! Being new to PHP, I was not aware of this array fact. It is a cool one to know and I think your method will get me where I need to be.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top