Forum Moderators: coopster

Message Too Old, No Replies

foreach vs for(.)

should i use foreach or for?

         

hypetheherpe

6:22 pm on Sep 5, 2005 (gmt 0)

10+ Year Member



i'm somewhat new to php... not really new to php, just don't know everything that can be done.. i learn as i need to do new things ya know lol.. but anyways.. yesterday i realized i could use a foreach statement when dealing with arrays... but in my code, i already have for statements being used.. so i'm wondering, which would be the correct way to go, or more correct way maybe? or is there no difference, it's just your preference? here's what i have..


for ($x=0; $x<=$ipnum; $x++) {
$lngperm = ip2long($permip[$x]);
if ($lngclient == $lngperm) {
$isnew = "false";
}
}

all this is saying is for x=0, continue to loop while you're less than or equal to the number of ip's in the array and after each loop, add 1 to x.. then $lngperm converts $permip[$x] (actual ip in the list $ipnum is counting) into a long ip.. and if the client's ip (converted to a long ip) equals the ip from the list (also converted to long), then set the variable $isnew to equal false.. all pretty basic.. but now that i could use foreach statements, i came up with the following code which does the same thing..


foreach ($permip as $listip) {
$lngperm = ip2long($listip);
if ($lngperm == $lngclient) {
$isnew = "false";
}
}

so basically my question is which method would be more proper to use and why? just because of less lines or is one function quicker than the other or more efficient or what? thanks for any help

bedlam

7:26 pm on Sep 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As far as I know, it's mainly a matter of preference. However there's one thing in your sample code that points to why one may be better than the other in certain circumstances:

You've got this:

for ($x=0; $x<=$ipnum; $x++) { ...

...which you've correctly paraphrased as this:

continue to loop while you're less than or equal to the number of ip's in the array...

However, unless the variable $ipnum is calculated in a way something like this:

$ipnum = count($permip) - 1;

...then your 'for' loop is cycling one time too many, since both arrays and the loop itself start counting at zero (and therefore should never count as high as the total number of objects in an array).

With the 'foreach' type of loop, this issue doesn't arise since you don't have to concern yourself with explicitly calculating how many items are in the array...

-B

hypetheherpe

8:23 pm on Sep 5, 2005 (gmt 0)

10+ Year Member



yep..

$ipnum = (count($permip) - 1); //count ips - 1 (array starts at 0)

thanks for pointing that out though.. and i have replaced the for (..) statement with a foreach(..) statement... i figured why not.. if there's something made specificly for what you're doing and something else that *could* do the job.. why not use what's made specificly for that job? :).. and less variables that way ($x isn't used).. thanks for the reply

jatar_k

4:33 pm on Sep 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld hypetheherpe,

also remember that your test is evaluated on every iteration

$x<=$ipnum

isn't too bad but it will still may be a fraction slower than a foreach. If you get into more complex tests then you may start noticing more difference in total execution time.