Re: For ... <= max($myArray) vs For ... <= $max

From: Date: Fri, 08 Nov 2002 19:00:28 +0000
Subject: Re: For ... <= max($myArray) vs For ... <= $max
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-123463@lists.php.net to get a copy of this message
If you reed the code carefully, it first assigns $x=1 and $max=count($myArray), then it compares $x with $max .: B i g D o g :. wrote:
Then only problem with doing it like for($x = 1, $max = count($myArray); $x <= $max; $x++ ) is that the $max = count( $myArray ) is always verified in each loop...slowing the for loop down... faster to do $max = count( $myArray ); for( $x = 1, $x <= $max; $x++ ) just my $0.02...for the day... On Fri, 2002-11-08 at 15:57, Kjartan Mannes wrote:
Friday, November 8, 2002, 12:13:01 PM, Daevid Vincent wrote:
$max = max($myArray); for( $x = 1; $x <= $length; $x++ ) {}
     
-- OR --
     
for( $x = 1; $x <= max($myArray); $x++ ) {}
     
My gut instinct tells me since PHP is interpreted, that the top one is the better way to go, but with the Zend Optimizer, I don't know if PHP handles them the same way or not?
     
The first one is faster, but it depends on the site of the array and how often you call the loop. I prefer doing it like this though: for($x = 1, $max = count($myArray); $x <= $max; $x++ ) {} For some good optimization (and other) tips check out: http://phplens.com/lens/php-book/optimizing-debugging-php.php http://www.lerdorf.com/tips.pdf -- Kjartan <kjartan@zind.net> (http://natrak.net/) :: "Silence is one great art of conversation." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#123463) next »