Page 1 of 1

Getting a variable from a loop

Posted: Thu Feb 18, 2010 11:45 am
by klevis miho
I want to echo, before the loop, a variable that is inside the loop.

How can it be done?

Re: Getting a variable from a loop

Posted: Thu Feb 18, 2010 12:09 pm
by AbraCadaver
You'll have to be more specific and maybe show some code/pseudo code.

Re: Getting a variable from a loop

Posted: Thu Feb 18, 2010 3:55 pm
by klevis miho
for example to get the $i and echo it before the foreach, is it possible?

foreach($rows as $row) {
$i++;
}

Re: Getting a variable from a loop

Posted: Thu Feb 18, 2010 3:57 pm
by Eran
What would be the value of $i before going through the loop?

Re: Getting a variable from a loop

Posted: Thu Feb 18, 2010 4:27 pm
by klevis miho
$i=0;

Re: Getting a variable from a loop

Posted: Thu Feb 18, 2010 4:31 pm
by Eran
So:

Code: Select all

$i = 0;
echo $i;
foreach($rows as $row) {
   $i++;
}
Would print out '0', but I'm guessing that's not what you had in mind. Maybe you should share with us your specific issue