473,385 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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

Jul 17 '05 #1
35 3167
"jerrygarciuh" wrote:
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


In php you get warnings for usage of any undefined variable. I
personally find it to be a good thing. By putting a bit more code in
order not to get the warning, you make it possible for yourself to
catch some pesky bugs later. So pay now (a little) or pay later (a
lot more).

I am sure there are different opinions on this, but I am for "one
ounce of prevention"...

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-Opinions...ict151233.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=506408
Jul 17 '05 #2
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" <de*****@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

Jul 17 '05 #3
.oO(jerrygarciuh)
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.
Correct behaviour. What should it do instead? Silently ignore?
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
Could also be NULL, FALSE, -1 or whatever.
- declare as arrays the vars which will receive arrays from functions
Hmm?
- place conditionals around every foreach
Depends.
just to avoid the warning.
There should either be a defined empty return value or another kind of
error handling (for example return NULL and check with is_null()). This
has nothing to do with avoiding warnings, it's simply a matter of good
coding practice. What does your function in question return in case of
an error or no results - simply nothing?
Opinions?


You should fix your code. Warnings are a sure sign of badly written
code. Even notices might lead to really annoying bugs which are hard to
find. You should develop with error_reporting set to E_ALL and fix _all_
issues that cause warnings or notices. Anything else is buggy code.

Micha
Jul 17 '05 #4
In article <uEF3d.209573$4o.203087@fed1read01>, jerrygarciuh wrote:
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;
}

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.


It seems to me your opinion is flawed.

foreach works only on arrays, and will issue an error when you try to
use it on a variable with a different data type or an uninitialized
variable. (http://be2.php.net/foreach)
--
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #5
In article <6HF3d.209595$4o.121981@fed1read01>, jerrygarciuh wrote:
$data = whatever();
foreach ($data as $datum) {
// do stuff if there is stuff to do, if nothing to do move on without
warning
}

function whatever() {
$aryResults = stuff_that_may_or_may_not_get_results();
return $aryResults;
}

This looks very bad to you?


Yes. You should always be absolutely sure the data really is what you
think it is.

And if you really don't agree, and don't want php spitting out warnings,
http://www.php.net/error_reporting.

--
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #6

"jerrygarciuh" <de*****@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" <to**@NOSPAM.demon.co.uk> wrote in message
news:ci*******************@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" <de*****@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
>
>
>



Jul 17 '05 #7
Tim,

My point was to discuss the idea not to suggest that that would work. Did
you read the thread?

jg
"Tim Van Wassenhove" <eu**@pi.be> wrote in message
news:2r*************@uni-berlin.de...
In article <uEF3d.209573$4o.203087@fed1read01>, jerrygarciuh wrote:
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;
}

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.


It seems to me your opinion is flawed.

foreach works only on arrays, and will issue an error when you try to
use it on a variable with a different data type or an uninitialized
variable. (http://be2.php.net/foreach)
--
Tim Van Wassenhove <http://www.timvw.info>

Jul 17 '05 #8
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.

jg
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:ci*******************@news.demon.co.uk...

"jerrygarciuh" <de*****@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" <to**@NOSPAM.demon.co.uk> wrote in message
news:ci*******************@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" <de*****@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
>
>
>



Jul 17 '05 #9
On Sun, 19 Sep 2004 10:07:22 -0500, jerrygarciuh wrote:
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


I think you can put an @ symbol before the foreach to turn off warnings
for that instance of that loop

<?
$a = "string";
@foreach ($a as $thing){
echo "oops! $a is a string!\n";
}
?>

--
Jeffrey D. Silverman | je**********@jhu.edu
Website | http://www.newtnotes.com

Drop "PANTS" to reply by email

Jul 17 '05 #10
On Mon, 20 Sep 2004 17:33:13 -0400, Jeffrey Silverman wrote:
I think you can put an @ symbol before the foreach to turn off warnings
for that instance of that loop

<?
$a = "string";
@foreach ($a as $thing){
echo "oops! $a is a string!\n";
}
?>


Ooops.

Nope!

I tried it. Only works for functions. Sorry about that.

--
Jeffrey D. Silverman | je**********@jhu.edu
Website | http://www.newtnotes.com

Drop "PANTS" to reply by email

Jul 17 '05 #11
.oO(jerrygarciuh)
What i was thinking was in this vein:

$data = whatever();
foreach ($data as $datum) {
// do stuff if there is stuff to do, if nothing to do move on without
warning
}

function whatever() {
$aryResults = stuff_that_may_or_may_not_get_results();
return $aryResults;
}

This looks very bad to you?


Yes. Your foreach-loop relies on a valid return value from whatever(),
so you have two possible solutions (besides of ignoring the warning,
which I wouldn't recommend):

1) The function should _always_ return an array, an empty one in case of
no result would IMHO make the most sense:

function whatever() {
return $aryResults = stuff_that_may_or_may_not_get_results()
? $aryResults
: array();
}

2) You have to check before entering the loop:

if ($data = whatever()) {
foreach ($data as $datum) {
// ...
}
}

Micha
Jul 17 '05 #12
"jerrygarciuh" <de*****@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?


Would be nice if PHP would convert a scalar to an array of one element when
it's used in an array context. Functions that can accept either would be a
lot simpler to write. Would make it a lot easier to work with an XML
document as an object too.
Jul 17 '05 #13
.oO(Tim Van Wassenhove)
foreach works only on arrays, [...]


.... and traversable objects (PHP5).

class TFoo implements IteratorAggregate {
// ...
}

foreach (new TFoo() as $foo) {
// ...
}

Micha
Jul 17 '05 #14

"jerrygarciuh" <de*****@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" <to**@NOSPAM.demon.co.uk> wrote in message
news:ci*******************@news.demon.co.uk...

"jerrygarciuh" <de*****@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" <to**@NOSPAM.demon.co.uk> wrote in message
> news:ci*******************@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" <de*****@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
>> >
>> >
>> >
>>
>>
>
>



Jul 17 '05 #15
On Tue, 21 Sep 2004 10:21:22 +0100, Tony Marston wrote:
"jerrygarciuh" <de*****@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.


This is true in strongly-typed languages, but as one of the PHP's points
was that it is a very weakly typed language, I see a valid point in jerry's
and OP's comments. Personally, the warning should be demoted to the rank of
notice.

Berislav
Jul 17 '05 #16

"Berislav Lopac" <be************@dimedia.hr> wrote in message
news:1a*******************************@40tude.net. ..
On Tue, 21 Sep 2004 10:21:22 +0100, Tony Marston wrote:
"jerrygarciuh" <de*****@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.


This is true in strongly-typed languages, but as one of the PHP's points
was that it is a very weakly typed language, I see a valid point in
jerry's
and OP's comments. Personally, the warning should be demoted to the rank
of
notice.


Where a function requires a particular data type then it is bad programming
to supply a variable of the wrong type. This is true of any language, not
just PHP.

In a user-defined function you may include code to check the type of each
variable and do whatever is necessary depending on the circumstances, but
for PHP's inbuilt function you must supply variables of the right type. This
is not a problem for competent programmers.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #17
> This is true in strongly-typed languages, but as one of the PHP's points
was that it is a very weakly typed language, I see a valid point in
jerry's and OP's comments. Personally, the warning should be demoted to
the rank of notice.


I agree. In fact, take it one step further. Notice should only be required
if supplied variable is undefined. This is the way it works with almost
every other expression (if, for, while, etc.) - foreach should not be an
exception.
Jul 17 '05 #18
On Sun, 19 Sep 2004 10:07:22 -0500, "jerrygarciuh"
<de*****@no.spam.nolaflash.com> wrote:
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.
I think it's reasonable.
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
If a function is supposed to return an array, then an empty array is a
perfectly good array, and also lets you use the idiom of a bool(false) return
indicating an error.
- declare as arrays the vars which will receive arrays from functions
Well, since the return value from the function will overwrite the type of the
original declaration, this doesn't help.

But if I have a loop that appends values to an array ($a[] = $something) then
I definitely tend to have $a = array() before I start the loop so it starts off
as an empty array.
- place conditionals around every foreach
just to avoid the warning.


I think you just have to be consistent in what's an array and what's not. PHP
lets you be pretty lazy with regards to types, so a little bit of discipline
with arrays is not such a bad thing IMHO.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #19
Tony Marston wrote:
Where a function requires a particular data type then it is bad
programming to supply a variable of the wrong type. This is true of any
language, not just PHP.
I would agree with functions, but foreach is just a control structure, like
"if" or "while." So, while you can easily do:

$val = string_or_zero();
if($val) //val is a non-empty string, do something
else //do something else

you cannot do:

foreach($val as $key=>$v) {}

In the first case, PHP tries to resolve it automatically, but produces a
warning in the second. I think both cases should be handled in a similar
manner.
In a user-defined function you may include code to check the type of each
variable and do whatever is necessary depending on the circumstances, but
for PHP's inbuilt function you must supply variables of the right type.
This is not a problem for competent programmers.


Again, you have a point about functions.
Jul 17 '05 #20
I do not see the difference between a control structure and a function. They
are both commands that are built into PHP. Foreach() expects an array, so if
you do not supply an array it complains about it. If you don't like the
complaint then do what the manual says and supply it with an array. The
authors of PHP are not going to change it just for you, so learn to live
with it.

--
Tony Marston

http://www.tonymarston.net

"Zurab Davitiani" <ag*@mindless.com> wrote in message
news:xj*************@newssvr13.news.prodigy.com...
Tony Marston wrote:
Where a function requires a particular data type then it is bad
programming to supply a variable of the wrong type. This is true of any
language, not just PHP.


I would agree with functions, but foreach is just a control structure,
like
"if" or "while." So, while you can easily do:

$val = string_or_zero();
if($val) //val is a non-empty string, do something
else //do something else

you cannot do:

foreach($val as $key=>$v) {}

In the first case, PHP tries to resolve it automatically, but produces a
warning in the second. I think both cases should be handled in a similar
manner.
In a user-defined function you may include code to check the type of each
variable and do whatever is necessary depending on the circumstances, but
for PHP's inbuilt function you must supply variables of the right type.
This is not a problem for competent programmers.


Again, you have a point about functions.

Jul 17 '05 #21
Tony Marston wrote:
I do not see the difference between a control structure and a function.
Programing 101:

if, else, for, while, do-while, try, etc. etc. are control structures. For
PHP-specific info, have a look at:
http://www.php.net/manual/en/languag...structures.php

as opposed to functions:
http://www.php.net/manual/en/funcref.php
They are both commands that are built into PHP. Foreach() expects an
array, so if you do not supply an array it complains about it.
And if() expects a boolean value but successfully deals with other types as
well. All I am saying it is inconsistent.
If you
don't like the complaint then do what the manual says and supply it with
an array. The authors of PHP are not going to change it just for you, so
learn to live with it.


I didn't ask anybody to change it "just for me;" I have an opinion and feel
free to express it in a public newsgroup - so you learn to live with it!
Jul 17 '05 #22
On Tue, 21 Sep 2004 23:37:19 GMT, Zurab Davitiani <ag*@mindless.com> wrote:
They are both commands that are built into PHP. Foreach() expects an
array, so if you do not supply an array it complains about it.


And if() expects a boolean value but successfully deals with other types as
well. All I am saying it is inconsistent.


That's because there are implicit conversions from all the basic types down to
the simple boolean type. But there aren't implicit conversion up to the array
type. You can explicitly cast to array if you want, though.
http://www.php.net/manual/en/languag....array.casting

Basic types get converted to an array containing that value as the only
element. Undefined/NULL values get converted to empty array.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #23
Andy Hassall wrote:
That's because there are implicit conversions from all the basic types
down to
the simple boolean type. But there aren't implicit conversion up to the
array type. You can explicitly cast to array if you want, though.
http://www.php.net/manual/en/languag....array.casting
Basic types get converted to an array containing that value as the only
element. Undefined/NULL values get converted to empty array.


That's a good point since an implicit conversion to array type would be
confusing as to whether the user wants to create an array with the variable
as its element, or skip the non-array type altogether; especially if you
consider how it deals with objects already:

foreach will take an object without any warnings/notices and will just
iterate through its properties.
Jul 17 '05 #24

"Zurab Davitiani" <ag*@mindless.com> wrote in message
news:PW***************@newssvr14.news.prodigy.com. ..
Tony Marston wrote:
I do not see the difference between a control structure and a function.


Programing 101:

if, else, for, while, do-while, try, etc. etc. are control structures. For
PHP-specific info, have a look at:
http://www.php.net/manual/en/languag...structures.php

as opposed to functions:
http://www.php.net/manual/en/funcref.php


That is just semantics. They are commands that are built into PHP, so from
that point of view they are no different. A user-defined function IS
different because you have complete control over what it does.
They are both commands that are built into PHP. Foreach() expects an
array, so if you do not supply an array it complains about it.


And if() expects a boolean value but successfully deals with other types
as
well. All I am saying it is inconsistent.
If you
don't like the complaint then do what the manual says and supply it with
an array. The authors of PHP are not going to change it just for you, so
learn to live with it.


I didn't ask anybody to change it "just for me;" I have an opinion and
feel
free to express it in a public newsgroup - so you learn to live with it!


You asked that it be changed to suit your preferences, therefore *just for
you* is valid. As it does not seem to bother the vast majority of PHP
programmers it is unlikely to be changed. So learn to live with it.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #25
Tony Marston wrote:
That is just semantics. They are commands that are built into PHP, so from
that point of view they are no different. A user-defined function IS
different because you have complete control over what it does.
Not just semantics - they are different in nature. Control structures
control the flow of the program, while functions perform certain specific
tasks.
You asked that it be changed to suit your preferences, therefore *just for
you* is valid. As it does not seem to bother the vast majority of PHP
programmers it is unlikely to be changed. So learn to live with it.


I reiterate that I didn't ask anyone to change anything. Falsely claiming
that I did something (that I didn't do) multiple times does not make it so.
Expressing an opinion or a preference != asking for a change.
Jul 17 '05 #26

"Zurab Davitiani" <ag*@mindless.com> wrote in message
news:%5***************@newssvr14.news.prodigy.com. ..
Tony Marston wrote:
That is just semantics. They are commands that are built into PHP, so
from
that point of view they are no different. A user-defined function IS
different because you have complete control over what it does.


Not just semantics - they are different in nature. Control structures
control the flow of the program, while functions perform certain specific
tasks.


The fact that control structures are diferent from functions is irrelevant
in this context. They are all commands/statements/verbs which are built into
PHP as standard. If one of these commands/statements/verbs is documented as
having an argument which is an array then it is bad programing to supply an
argument which is not an array, and bad manners to then complain that PHP
issues a warning/error message.
You asked that it be changed to suit your preferences, therefore *just
for
you* is valid. As it does not seem to bother the vast majority of PHP
programmers it is unlikely to be changed. So learn to live with it.


I reiterate that I didn't ask anyone to change anything. Falsely claiming
that I did something (that I didn't do) multiple times does not make it
so.
Expressing an opinion or a preference != asking for a change.


Your statement "I think both cases should be handled in a similar manner"
goes beyond expressing an opinion or a preference. You are stating that
PHP's behaviour should be changed.

--
Tony Marston

http://www.tonymarston.net


Jul 17 '05 #27
Tony Marston wrote:
The fact that control structures are diferent from functions is irrelevant
in this context. They are all commands/statements/verbs which are built
into PHP as standard. If one of these commands/statements/verbs is
documented as having an argument which is an array then it is bad
programing to supply an argument which is not an array, and bad manners to
then complain that PHP issues a warning/error message.
I already gave examples to the contrary in this thread. I am not going to go
back and copy and paste stuff just because you have chosen to ignore them.
Your statement "I think both cases should be handled in a similar manner"
goes beyond expressing an opinion or a preference. You are stating that
PHP's behaviour should be changed.


Nonsense! If I wanted to request PHP behavior to change, I would definitely
NOT be replying to you - I'd be posting my request to PHP developers
mailing list. In case of any further confusion, the phrase "I think" refers
to my personal opinions and is not a request or demand to anyone -
hopefully most people realize that.
Jul 17 '05 #28

"Zurab Davitiani" <ag*@mindless.com> wrote in message
news:sG**************@newssvr13.news.prodigy.com.. .
Tony Marston wrote:
The fact that control structures are diferent from functions is
irrelevant
in this context. They are all commands/statements/verbs which are built
into PHP as standard. If one of these commands/statements/verbs is
documented as having an argument which is an array then it is bad
programing to supply an argument which is not an array, and bad manners
to
then complain that PHP issues a warning/error message.


I already gave examples to the contrary in this thread. I am not going to
go
back and copy and paste stuff just because you have chosen to ignore them.


You have not given any examples where an expected argument of type=array can
be supplied as not=array and not generate any warnings/errors.

The if($var) statement will work on anything and evaluate it to either TRUE
or FALSE. It does not insist on a particular variable type. This is asa
documented.

Foreach() however is for working on arrays, so if you give it a non-array
then it is right to complain. This is as documented.
Your statement "I think both cases should be handled in a similar manner"
goes beyond expressing an opinion or a preference. You are stating that
PHP's behaviour should be changed.


Nonsense! If I wanted to request PHP behavior to change, I would
definitely
NOT be replying to you - I'd be posting my request to PHP developers
mailing list. In case of any further confusion, the phrase "I think"
refers
to my personal opinions and is not a request or demand to anyone -
hopefully most people realize that.


The word *should* is imperative which implies more than wishfull thinking.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #29
Tony Marston <to**@NOSPAM.demon.co.uk> wrote:
Your statement "I think both cases should be handled in a similar manner"
goes beyond expressing an opinion or a preference. You are stating that
PHP's behaviour should be changed.


Nonsense! If I wanted to request PHP behavior to change, I would
definitely
NOT be replying to you - I'd be posting my request to PHP developers
mailing list. In case of any further confusion, the phrase "I think"
refers
to my personal opinions and is not a request or demand to anyone -
hopefully most people realize that.


The word *should* is imperative which implies more than wishfull thinking.


I understood his statement the way he intended it... (I would controvert
it, too, but that's not the topic.)
How would *you* express it, if not this way?

Btw: Neither this group is called soc.land.english, nor everyone here is a
native speaker.

--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #30
Tony Marston wrote:
You have not given any examples where an expected argument of type=array
can be supplied as not=array and not generate any warnings/errors.

The if($var) statement will work on anything and evaluate it to either
TRUE or FALSE. It does not insist on a particular variable type. This is
asa documented.

Foreach() however is for working on arrays, so if you give it a non-array
then it is right to complain. This is as documented.
foreach($object as $value)
The word *should* is imperative which implies more than wishfull thinking.


And Merry Christmas to you too!
Jul 17 '05 #31

"Zurab Davitiani" <ag*@mindless.com> wrote in message
news:AN***************@newssvr14.news.prodigy.com. ..
Tony Marston wrote:
You have not given any examples where an expected argument of type=array
can be supplied as not=array and not generate any warnings/errors.

The if($var) statement will work on anything and evaluate it to either
TRUE or FALSE. It does not insist on a particular variable type. This is
asa documented.

Foreach() however is for working on arrays, so if you give it a non-array
then it is right to complain. This is as documented.


foreach($object as $value)


Your point being? An object can be treated as an array of values. NULL is
not an array, A string is not an array. A number is not an array.
The word *should* is imperative which implies more than wishfull
thinking.


And Merry Christmas to you too!


Musseltoff.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #32
.oO(Tony Marston)
"Zurab Davitiani" <ag*@mindless.com> wrote
foreach($object as $value)


Your point being? An object can be treated as an array of values.


Maybe Zurab is referring to PHP5's new feature of iterating over objects
which implement one of the interfaces Iterator or IteratorAggregate.
These objects are _not_ treated as arrays, foreach() directly calls
their methods.

Micha
Jul 17 '05 #33
Tony Marston <to**@nospam.demon.co.uk> wrote:
Your point being? An object can be treated as an array of values. NULL is
not an array, A string is not an array. A number is not an array.


And exactly how is string not an array? It has indexes, elements and a
length, it surely acts like an array.

<?php
$str="foo";
echo "Char at index 1: {$str[1]}\n";

$str[4]='b';
$str[5]='a';
$str[6]='r';
$str[7]="\n";

print_r($str);
?>

Thou [] shouldn't be used :)

--

Daniel Tryba

Jul 17 '05 #34

"Daniel Tryba" <ne****************@canopus.nl> wrote in message
news:ci**********@news.tue.nl...
Tony Marston <to**@nospam.demon.co.uk> wrote:
Your point being? An object can be treated as an array of values. NULL is
not an array, A string is not an array. A number is not an array.
And exactly how is string not an array? It has indexes, elements and a
length, it surely acts like an array.


Then try using foreach() on a string and see what happens. The whole point
of this thread is when foreach() is given an argument which is not an
array - should it throw a warning or not?

--
Tony Marston

http://www.tonymarston.net
<?php
$str="foo";
echo "Char at index 1: {$str[1]}\n";

$str[4]='b';
$str[5]='a';
$str[6]='r';
$str[7]="\n";

print_r($str);
?>

Thou [] shouldn't be used :)

--

Daniel Tryba

Jul 17 '05 #35
Tony Marston <to**@nospam.demon.co.uk> wrote:
Your point being? An object can be treated as an array of values. NULL is
not an array, A string is not an array. A number is not an array.


And exactly how is string not an array? It has indexes, elements and a
length, it surely acts like an array.


Then try using foreach() on a string and see what happens. The whole point
of this thread is when foreach() is given an argument which is not an
array - should it throw a warning or not?


IMHO it should (and thus I personally always check for the correct
type). But an object is as much an array as a string is, so an abject
should fail to.

--

Daniel Tryba

Jul 17 '05 #36

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: rzed | last post by:
Periodically, we see questions about which gui package is best, or which database package to use. These questions typically trigger some exchanges of opinion, though of course this no one best...
13
by: cody | last post by:
foreach does implicitly cast every object in the collection to the specified taget type without warning. Without generics this behaviour had the advantage of less typing for us since casting was...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
4
by: atyndall | last post by:
OK, this is the relevant portion script: <?php $username = '__'; // MySQL Database Username. $password = '__'; // MySQL Database Password. $server = '__'; // MySQL Database server (most...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.