NYCPHP Meetup

NYPHP.org

[nycphp-talk] foreach ERROR

Rob Marscher rmarscher at beaffinitive.com
Mon Jul 17 17:21:08 EDT 2006


I'm pretty certain what's happening here is that the glob function is 
returning an empty array which is why the foreach on line 4 doesn't give 
an error, but it also doesn't set $sort to anything.

foreach requires that the first argument is an array.  So that's why you 
get errors on lines 5 and 18.  You should add a condition between line 4 
and 5 that does something like "if (! is_array($sort))" and then 
displays an empty xml doc and exits if that is the case.

-Rob

CED wrote:
> www.EdwardPrevost.info/Audio <http://www.EdwardPrevost.info/Audio>
>  
> You'll see the basic structure there
> /Music is the MP3 files
> /mp3 is the PHP file
>  
> The error is a basic WARNING Invalid argument supplied for foreach() on 
> line 5. Same thing reported for Line 18.
> 
>     ----- Original Message -----
>     *From:* lists at jack-scott.com <mailto:lists at jack-scott.com>
>     *To:* NYPHP Talk <mailto:talk at lists.nyphp.org>
>     *Sent:* Monday, July 17, 2006 4:11 PM
>     *Subject:* Re: [nycphp-talk] foreach ERROR
> 
> 
>     On Mon, 2006-07-17 at 15:43 -0400, CED wrote:
>>     Sirs, 
>>     I have used variables many times in foreach statements... and
>>     having spent several hours tryignt o debug this I thought I'd
>>     throw it out here: 
>>
>>        1. <?
>>        2. function readD($dir){
>>        3. if(!$dir){$dir = '.';}
>>        4. foreach(glob("$dir/*.mp3") as $item){$sort[]=
>>           end(explode('/',$item));}
>>        5.  foreach($sort as $sorteditem){
>>        6.   if($sort){natsort($sort);}
>>        7.     foreach($sort as $item){$return[]= $item;}
>>        8.      if(!$return){return array();}
>>        9.      }
>>       10.   return $return;
>>       11.   } ?>
>>       12. <?php header("Content-type: text/xyxml") ?>
>>       13. <? echo(' <?xml version="1.0" encoding="UTF-8"?> ') ; ?>
>>       14. <? echo '<player showDisplay="yes" showPlaylist="no"
>>           autoStart="yes">
>>       15. ' ;
>>       17. $sFolder = '../Audio/Music' ;
>>       18. foreach(readD($sFolder) as $item)
>>       19. { 
>>     PHP keeps erroring on Lines 5 and 18. Any thoughts? Is this a bug?
>>     Am I a moron?
> 
>     what are the errors? Is line five looping on an empty array
> 
>     ------------------------------------------------------------------------
> 
>     _______________________________________________
>     New York PHP Community Talk Mailing List
>     http://lists.nyphp.org/mailman/listinfo/talk
> 
>     NYPHPCon 2006 Presentations Online
>     http://www.nyphpcon.com
> 
>     Show Your Participation in New York PHP
>     http://www.nyphp.org/show_participation.php
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
> 
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
> 
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php



More information about the talk mailing list