Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?

From: Tony Marston (tony_at_NOSPAM.demon.co.uk)
Date: 09/21/04


Date: Tue, 21 Sep 2004 10:21:22 +0100


"jerrygarciuh" <designs@no.spam.nolaflash.com> wrote in message
news:1HH3d.210042$4o.44641@fed1read01...
> Tony,
>
> It was never a question of what PHP does do in this circumstance. I just
> posited the idea that the warning seems TO ME to be unnecessary and
> annoying
> in this particular context.
>

It may seem unnecesary and annoying to you, but it is a perfectly valid
error message. If a function is supposed to return an array but it has
nothing to put in it then it should return an empty array, not a null value.
Likewise if any PHP function expects an array and you give it something else
then PHP is correct in reporting it as an error.

-- 
Tony Marston
http://www.tonymarston.net
>
> "Tony Marston" <tony@NOSPAM.demon.co.uk> wrote in message
> news:cine3u$bqp$1$8302bc10@news.demon.co.uk...
>>
>> "jerrygarciuh" <designs@no.spam.nolaflash.com> wrote in message
>> news:uEF3d.209573$4o.203087@fed1read01...
>> > Tony,
>> >
>> > So for example what would be so bad about this:
>> >
>> > $data = whatever();
>> > foreach ($data as $datum) {
>> >    // do stuff if there is stuff to do
>> > }
>> >
>> > function whatever() {
>> >    $aryResults = stuff_that_may_or_may_not_get_results();
>> >    return $aryResults;
>> > }
>>
>> If function stuff_that_may_or_may_not_get_results() is supposed to return
> an
>> array then if it finds nothing it should return an emty array, not
> nothing.
>>
>> I assume you do know the difference between an empty array and nothing?
>>
>> $var = null;
>> $var = array();
>>
>> > It seems to me in this context foreach() ought to iterate over $data 
>> > and
>> > finding nothing just go on to the next block in the script.
>> >
>>
>> foreach() is used to ieterate over an array. If you do not supply an 
>> array
>> then it is you who are wrong.
>>
>> --
>> Tony Marston
>>
>> http://www.tonymarston.net
>>
>>
>>
>> >
>> > "Tony Marston" <tony@NOSPAM.demon.co.uk> wrote in message
>> > news:cikis0$117$1$8300dec7@news.demon.co.uk...
>> >> It is very BAD programming to use a variable for input which has yet 
>> >> to
>> >> be
>> >> declared, so it is a GOOD idea to use isset() before you attempt to
>> >> reference such a variable.
>> >>
>> >> --
>> >> Tony Marston
>> >>
>> >> http://www.tonymarston.net
>> >>
>> >>
>> >>
>> >> "jerrygarciuh" <designs@no.spam.nolaflash.com> wrote in message
>> >> news:Lgh3d.27014$ni.11050@okepread01...
>> >> > Hi all,
>> >> >
>> >> > I was just wondering what popular opinion is on PHP giving this
>> >> > warning:
>> >> >
>> >> > Warning: Invalid argument supplied for foreach() in
>> >> > /home/boogerpic/public_html/my.php on line 6
>> >> >
>> >> > when presented with an indefined variable.
>> >> >
>> >> > It makes sense to me to warn if an unacceptably defined var is 
>> >> > passed
>> > but
>> >> > it
>> >> > does not seem _GOOD_ to me to have to
>> >> >
>> >> > - have my functions return empty arrays
>> >> > - declare as arrays the vars which will receive arrays from 
>> >> > functions
>> >> > - place conditionals around every foreach
>> >> >
>> >> > just to avoid the warning.
>> >> >
>> >> > Opinions?
>> >> >
>> >> > jg
>> >> >
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >
>>
>>
>
> 


Relevant Pages

  • Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?
    ... It was never a question of what PHP does do in this circumstance. ... posited the idea that the warning seems TO ME to be unnecessary and annoying ... > array then if it finds nothing it should return an emty array, ... > Tony Marston ...
    (comp.lang.php)
  • Re: Need help on PHP for MPE/ix
    ... Here is the syntax of the dbupdate intrinsic in PHP: ... assoc array of item values) ... Please let me know if you have any more suggesstions to solve this problem. ... Pavan Kumar Rati wrote: ...
    (comp.sys.hp.mpe)
  • Re: The quest for opensource database...
    ... "Tony Marston" wrote in message ... > I like to keep all my code in PHP modules rather than spread them over ... > Debugging triggers or procedures is not easy, so if you have a problem ... I find SPs are useful when invoking complex routine queries against the dbms ...
    (comp.lang.php)
  • Re: Using a RegEx as a "variable" WITHIN an array?
    ... believe the PHP one is also based upon. ... combination of 1 or more numerics in this instance would give you your ... add this to your array - you could then do a pattern match on the array ... > Dim selCriteria as String ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Need help on PHP for MPE/ix
    ... If one item is an array, it must be declared as such in the list ... So, please show us the declaration of the record buffer, and its ... Of Pavan Kumar Rati ... Need help on PHP for MPE/ix ...
    (comp.sys.hp.mpe)

Loading