Jump to content

for loop of variables


malikah

Recommended Posts

I have a list of variables:

 

 

$varibl01="abc";

$varibl02="def";

$varibl03="ghi";

$varibl04="jkl";

$varibl05="mno";

 

and I want to access each of them in a for loop:

 

for($i=0;$i<5;$i++){

echo $varibl0x //where "x" is $i - is there a way to do this?

}

 

Cheers.

Link to comment
Share on other sites

Is there no way you can make this an array and have

$varibl = array();
$varibl[01]="abc";
$varibl[02]="def";
$varibl[03]="ghi";
$varibl[04]="jkl";
$varibl[05]="mno";

 

then you can simply use:

 

foreach($varibl as $value) {
echo $value;
}

 

There is a way to use the eval(); function but thats not very trustworthy coding.

Link to comment
Share on other sites

while ($row = mysql_fetch_row($xyzQuery))
{
    foreach ($row as $val) {
         echo $val;
    }
}

 

That makes it easier =)

I don't want to change the original code - I want to access each of the $a's in a for loop by referring to their last digit.
Link to comment
Share on other sites

Instead why not just access them via the array index???

 

while ($row = mysql_fetch_row($xyzQuery))
{
    for ($i=0;$i<5;$i++) {
         echo $row[$i];
    }
}

 

Same principle, just easier and more efficient to do....

 

What is your logic behind assigning them to variables and not just using the array index?

Link to comment
Share on other sites

Because I don't know what $row is - what happened to the list?

 

The list is not needed, that just broke out the array.

 

$row is an array that contains all the data that list broke out inside of it's self to be accessed as shown above.

 

I would suggest reading up on array's They are like a storage container for data to be easily access/iterated through.

Link to comment
Share on other sites

If you want to refer to them by there last digits then you need to take the sub string from the actual variable name.  This, for example, will display all the variables with a 1 at the end of the variable.

 

*NOT TESTED*

 

while ($row = mysql_fetch_row($xyzQuery))
{
    for ($i=0;$i         if(substr($$row[$i], 1) == 1) {
             echo $row[$i];
         }
    }
}

 

or you can access by first letter...

 

$string = $$row[$i];
if($string[1] == 'a') {
    echo $row[$i];
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.