Home > Archive > PHP on Windows > September 2006 > Re: [PHP-WIN] Re: [Linux dev] Undefined variables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: [PHP-WIN] Re: [Linux dev] Undefined variables
|
|
|
| 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('.');
// 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] />";
}
| |
| Alex Turner 2006-09-08, 6:58 pm |
| 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('.');
> // 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] />";
> }
--
www.deployview.com
www.nerds-central.com
www.project-network.com
| |
|
| Please reply to the list not just directly to me.
Alf Stockton wrote:
> Stut wrote:
>
> .....snip.....
> Part of the backwards iteration. In other words I was using the array
> from the back to the front rather than from 0 through end.
> Thank you for your input and code.
> I do find it interesting that the program I had worked fine on versions
> of Windows PHP previous to 5.1.6.
The script you had was working, but it was throwing out notices. Notices
are a level of message below warnings that indicate that you're doing
something that can lead to bugs but that PHP can cope with. They can be
disabled and are on a shocking number of PHP installations I come across.
When you say it "worked fine" it's probably because the PHP
configuration had notices disabled. This is bad m'kay. With notices
enabled you may end up writing a bit more code, but you also get told
when you do things like mis-type variable names and the like.
Look in the PHP manual for the error_reporting php.ini setting. The
knowledge you s will be found there.
-Stut
|
|
|
|
|