Re: sort() warning

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2006-09-06 at 10:47 -0600, Ashley M. Kirchner wrote:
>     Given this piece of code:
> 
>   $i = 0;
>   if ($dir = opendir($path)) {
>     while ($dh = readdir($dir)) {
>       if ($dh != '.' && $dh != '..') {
>         $Dirs[$i] = $dh;
>         $i++;
>       }
>     }
>   }
>   closedir($dir);
>   sort($Dirs);
> 
>     Why does sort() give me the following warning:
> 
>     PHP Warning:  sort() expects parameter 1 to be array, null given in ....
> 
>     The array contents is as follows ( according to print_r($Dirs) )
> 
>     Array
>     (
>         [0] => 1029
>         [1] => 1197
>         [2] => 1254
>         [3] => 1093
>         [4] => 1217
>         [5] => 1272
>         [6] => 1233
>         [7] => 1257
>         [8] => 1017
>         [9] => 1033
>     )
> 
> 
>     The end result I want is that it sorts out that array in ascending 
> order, thus 1017, 1029, 1033, 1042, etc., etc...

It's because you have written sloppy code and didn't bother to
initialize $Dirs to an array. So it's default value is null. You would
know this if you had notices enabled.

Also, the other problem is that you are either a) opening the wrong
path, b) the path you are opening has no files or directories.

Take note also that your variable is called $Dirs, but readdir() returns
all directory contents (including files, links, etc, etc). So either you
don't understand readdir() or you have a penchant for sloppy code as
denoted by your confusing variable nomenclature, lack of E_NOTICE, and
disregard for initializing variables. I would strongly suggest RTFM.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux