Getting a variable from a 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
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Getting a variable from a loop

Post by klevis miho »

I want to echo, before the loop, a variable that is inside the loop.

How can it be done?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Getting a variable from a loop

Post by AbraCadaver »

You'll have to be more specific and maybe show some code/pseudo code.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Getting a variable from a loop

Post by klevis miho »

for example to get the $i and echo it before the foreach, is it possible?

foreach($rows as $row) {
$i++;
}
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Getting a variable from a loop

Post by Eran »

What would be the value of $i before going through the loop?
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Getting a variable from a loop

Post by klevis miho »

$i=0;
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Getting a variable from a loop

Post 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
Post Reply