PHP consecutive variables

j79zlr

Glaanies script monkey
Political User
#2
I didn't try this, but if you used an array it would be easy:

PHP:
$variables = 10;
$i = 0;
while ($i < $variables) {
echo "$Track[$i]";
++$i;
}
 

j79zlr

Glaanies script monkey
Political User
#4
Your welcome, hardly genius, but I'll take it ;)

Oops, I had a booboo above, I'll edit it, I had ++$x where it should have been ++$i
 

j79zlr

Glaanies script monkey
Political User
#5
OK, just like any code, I think that there are acouple things that need to be changed;

PHP:
$variables = 10;
$i = 0;
while ($i <= $variables) {
echo "$Track[$i]";
++$i;
}
See, now I changed the loop so its 'less than or equal to' so it will print all instead of skipping the last one. Another problem is that the array numbering starts at 0 and not 1. You could always start at
Track[1]=Track 1 value
and so forth and change $i=0 to $i=1.
 

X-Istence

*
Political User
#7
arson_nick just doing

PHP:
$Trakc = array();
Will work fine.

j79zlr this:

PHP:
$variables = 10;
$i = 0;
while ($i <= $variables) {
echo "$Track[$i]";
$i++;
}
Note the $i++;, would in this example you gave, do the same as

PHP:
$variables = 10;
$i = 0;
while ($i <= $variables) {
echo "$Track[$i]";
++$i;
}
Even better code would be:

PHP:
foreach($Track as $value) {
echo $value;
}
This will let him loop thru the entire array, EVEN when its bigger than the 10 variables you had set it to :).

Check out http://php.net/foreach for more info.
 

X-Istence

*
Political User
#8
Now, if you did not have it in an array, then this would help:

PHP:
$x = 0;
while (1) {
$var = "Track" . $x;
echo ${$var};
$x++;
$var = "Track" . $x;
if (${$var} == "") {
break;
}
}
This will loop thru variables like this:

$Track1
$Track2
$Track3
$Track4
etc ...
 

Members online

No members online now.

Latest posts

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,971
Messages
673,299
Members
89,016
Latest member
Poseeut