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
- Next message: Berislav Lopac: "Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?"
- Previous message: Davor Pleskina: "PHP port 7869"
- In reply to: jerrygarciuh: "Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?"
- Next in thread: Berislav Lopac: "Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?"
- Reply: Berislav Lopac: "Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 >> >> > >> >> > >> >> > >> >> >> >> >> > >> > >> >> > >
- Next message: Berislav Lopac: "Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?"
- Previous message: Davor Pleskina: "PHP port 7869"
- In reply to: jerrygarciuh: "Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?"
- Next in thread: Berislav Lopac: "Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?"
- Reply: Berislav Lopac: "Re: Opinions? Is it _GOOD_ that foreach() throws warning when given undef var?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
Loading