Re: Re: [Linux dev] Undefined variables
| From: | Alex Turner | Date: | Fri, 08 Sep 2006 16:44:03 +0000 |
| Subject: | Re: Re: [Linux dev] Undefined variables | ||
| References: | 1 2 3 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-27137@lists.php.net to get a copy of this message | ||
Foreach is very much better as it does not mean having to traverse the array to find the given value. If you have a long array, you'll find looking up a element by index gets slow.
AJ
Stut wrote:
Hendrik Visage wrote: <snip useful explanation of what's wrong with no tips on how to do it right> The rewrite below does not iterate around the array as you were in your for loop, so I'll mention a better way to do that here rather than in the snippet below... foreach - look it up in the PHP manual! One way to rewrite it would be like this (with comments hopefully explaining what I've changed and why)... <?php session_start(); $folder = opendir('.');-- www.deployview.com www.nerds-central.com www.project-network.com// No need for a temporary array, check each file as you read it from the directory$names = array(); while ($file = readdir($folder)) {if (in_array(strtolower(substr($file, -4)), array('.jpg', '.gif', 'jpeg', '.png'))) { // You can append an element to an array like this... no need for an index $names[] = $file; }} sort($names); // Not sure why you're checking a session var here since you've just gone to the trouble of iterating // over the files - it's not saving much processing // Best practice to always check if a session var exists before using it // Avoids unsightly notices $Count = (isset($_SESSION['Picture']) ? $_SESSION['Picture'] : 0);// Really not sure why this is here--$Count;// echo 'Count = '.$Count.' $tempvar = '.$tempvar;if ($Count == 0) {$Count = count($names1)-1;} $_SESSION['Picture'] = $Count; $slika = $names[$Count]; // image dimensions $dimensions = GetImageSize($slika); if (isset($HTTP_GET_VARS["pic"])) {// Strictly speaking the URL given in a location header should be absolute header ("Location: $slika");} else {echo "<img src=\"$slika\" $dimensions[3] />";}