Forum Moderators: coopster
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
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
$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