RE: [PHP] For Loops and Variables??

2003-09-16 Thread Chris W. Parker
John Ryan mailto:[EMAIL PROTECTED]
on Saturday, September 13, 2003 9:26 AM said:

 When I use for loops, at the start of each iteration, the variables
 hold the values from the last loop.

You're doing it wrong then (I think).

1. You should always initialize your loop counters.

i.e.

$x = 0;

while($x  100)
{
$x++;
}

2. Don't use the same counter name in all your loops.

This is bad:

for($x=0;$x100;$x++)
{
for($x=0;$x100;$x++)
{
echo i'm going to go on forever!!;
}
}

This Is Good(tm):

for($x=0;$x100;$x++)
{
for($x=0;$x100;$x++)
{
echo Me happy now!!;
}
}



Does that help?

Chris.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] For Loops and Variables??

2003-09-16 Thread Chris W. Parker
Chris W. Parker 
on Tuesday, September 16, 2003 4:07 PM said:

 This Is Good(tm):
 
 for($x=0;$x100;$x++)
 {
   for($x=0;$x100;$x++)
   {
   echo Me happy now!!;
   }
 }

Hehe... whoops! Should be:

for($x=0;$x100;$x++)
{
for($y=0;$y100;$y++)
{
echo Me happy now!!;
}
}



BYE!

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] For Loops and Variables??

2003-09-13 Thread John Ryan
When I use for loops, at the start of each iteration, the variables hold the
values from the last loop.

First, is there an elegant way of clearing variables at the start of each
loop rather than using unset???! It just seems wrong.

Also, all my problems would be solved if variables in a for loop were kept
local, but everything by default is a global. Can I change this in php.ini
or something??

TIA

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] For Loops and Variables??

2003-09-13 Thread Curt Zirzow
* Thus wrote John Ryan ([EMAIL PROTECTED]):
 When I use for loops, at the start of each iteration, the variables hold the
 values from the last loop.
 
 First, is there an elegant way of clearing variables at the start of each
 loop rather than using unset???! It just seems wrong.

I'm unclear as to what elegant implies.

 
 Also, all my problems would be solved if variables in a for loop were kept
 local, but everything by default is a global. Can I change this in php.ini
 or something??

no, there are only two scopes in php, it is either GLOBAL or local inside
a function/class.


Curt
-- 
I used to think I was indecisive, but now I'm not so sure.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php