Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » PHP
  • » [PHP-DEV] Re: foreach with null [RSS Feed]

#1 Oct. 24, 2005 23:54:41

Christian S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Re: foreach with null


Marcus Boerger wrote:i would like to change foreach a tiny bit. Actually i don't like the
warning when using foreach with null.

The way we use NULL in PHP should result in no warning here. The patch forCan you elaborate on the "The way we use NULL in PHP" part?IMPW (In my PHP world) null represents an undefined variable. Trying toiterate over it could be a programming mistake. Like iterating over ascalar value. I normally use foreach ((array)$var as $value) if $var isof "mixed" type which also catches null. But most of the time I'm morecomfortable with making sure I provide a real array in all cases anddon't rely on magic.Just my two cents,
- Chris

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#2 Oct. 25, 2005 00:08:26

David Z.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Re: foreach with null


> IMPW (In my PHP world) null represents an undefined variable. Trying to
> iterate over it could be a programming mistake. Like iterating over a
> scalar value. I normally use foreach ((array)$var as $value) if $var is
> of "mixed" type which also catches null. But most of the time I'm more
> comfortable with making sure I provide a real array in all cases and
> don't rely on magic.

Good point. Smells like a -1 to me.

- David

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#3 Oct. 26, 2005 18:19:51

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Re: foreach with null


> Any opinions?

Good code is code that's easy to debug (among other things). By
removing the "Invalid argument supplied for foreach()" warning without
providing mechanisms to turn it on, debugging code will be more
difficult. Logically speaking, when does it make sense to iterate over
something that isn't an array?

If I were writing one-time use scripts that I will never use again
(not likely) this might be desired behavior. If I were writing code
that is used by other programmers, paying users, and under conditions
likely to change (implying changing code, and often debugging and
testing), the ability to detect errors as early as possible is vital
(The Pragmatic Programmer is a good reference for this). Most people
don't want to spend more time than is necessary to troubleshoot code.
Probably more importantly, managers and customers don't want to wait
longer for a product they're paying for.

Understandly, PHP aims to be "easy" in so many ways, but I believe in
order for PHP to survive in the enterprise and allow users to easily
write good code, it *must* support such features. Without features
like these, writing PHP won't be so easy because the work required to
do the checking is forced on the programmer.

The "Invalid argument supplied for foreach()" warning should not be
removed unless it can be done without breaking compatibility and with
a way to optionally turn it on/off. I think we can learn a good lesson
from Perl on this one. Here is how we turn on/off warnings, lexically:

use warnings;

- sebastian

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#4 Oct. 26, 2005 18:51:47

Markus F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Re: foreach with null


Sebastian wrote:Good code is code that's easy to debug (among other things). By
removing the "Invalid argument supplied for foreach()" warning without
providing mechanisms to turn it on, debugging code will be more
difficult. Logically speaking, when does it make sense to iterate over
something that isn't an array?

If I were writing one-time use scripts that I will never use again
(not likely) this might be desired behavior. If I were writing code
that is used by other programmers, paying users, and under conditions
likely to change (implying changing code, and often debugging and
testing), the ability to detect errors as early as possible is vital
(The Pragmatic Programmer is a good reference for this). Most people
don't want to spend more time than is necessary to troubleshoot code.
Probably more importantly, managers and customers don't want to wait
longer for a product they're paying for.I also don't feel very comfortable with silencing the warning for NULL.If the variable is not an array and not traversable there's nothing toiterate over and it should tell the user. If we intentionally want tonot know about this error, an alternate syntax with the '@' operatorshould be considered as it was requested in the past, e.g. '@foreach' or'foreach(@$notarrayOrIterable ...'.- Markus

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#5 Oct. 26, 2005 20:37:25

Jani T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Re: foreach with null


On Wed, 26 Oct 2005, Sebastian wrote:

Good code is code that's easy to debug (among other things). By
removing the "Invalid argument supplied for foreach()" warning without
providing mechanisms to turn it on, debugging code will be more
difficult.

You got to the point here. I'd like to see E_STRICT be used for this
if this NULL thing is accepted. Also, I found out recently something
that I didn't know about before and which I count as inconsistent:

count(<somethingthatisnotanemptyarraynorobjectnornull>) returns int(1).

It would be nice that count() would also produce an E_STRICT notice
when passed something else than object or array. Or rather make
every single function and language construct to produce the E_STRICT
notice when passed wrong type of variable.

--Jani

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#6 Oct. 26, 2005 20:43:36

Greg B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Re: foreach with null


Jani Taskinen wrote:
> On Wed, 26 Oct 2005, Sebastian wrote:
>
>
>
>> Good code is code that's easy to debug (among other things). By
>> removing the "Invalid argument supplied for foreach()" warning without
>> providing mechanisms to turn it on, debugging code will be more
>> difficult.
>
>
>
> You got to the point here. I'd like to see E_STRICT be used for this
> if this NULL thing is accepted. Also, I found out recently something
> that I didn't know about before and which I count as inconsistent:
>
> count(<somethingthatisnotanemptyarraynorobjectnornull>) returns int(1).
>
> It would be nice that count() would also produce an E_STRICT notice
> when passed something else than object or array. Or rather make
> every single function and language construct to produce the E_STRICT
> notice when passed wrong type of variable.

amen, this behavior makes no sense even though sizeof() is an alias to
count(). sizeof('this') and sizeof('this long thing') are both 1, which
makes no sense. I would go so far as to say a E_NOTICE is more
appropriate than E_STRICT - you should only be using count() for
arrays/objects.

Then Zend could remove the silly question from the cert example exam ;).

Greg

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#7 Oct. 26, 2005 22:25:11

Ilia A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Re: foreach with null


Greg Beaver wrote:
> amen, this behavior makes no sense even though sizeof() is an alias to
> count(). sizeof('this') and sizeof('this long thing') are both 1, which
> makes no sense. I would go so far as to say a E_NOTICE is more
> appropriate than E_STRICT - you should only be using count() for
> arrays/objects.

Actually this result makes perfect sense since type conversion changes
string into array('your string') and does a count of that, which is 1.

Ilia

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#8 Oct. 27, 2005 00:17:49

Greg B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Re: foreach with null


Ilia Alshanetsky wrote:
> Greg Beaver wrote:
>
>>amen, this behavior makes no sense even though sizeof() is an alias to
>>count(). sizeof('this') and sizeof('this long thing') are both 1, which
>>makes no sense. I would go so far as to say a E_NOTICE is more
>>appropriate than E_STRICT - you should only be using count() for
>>arrays/objects.
>
>
> Actually this result makes perfect sense since type conversion changes
> string into array('your string') and does a count of that, which is 1.

Yes, it makes sense from this perspective, but from a real-life
programming perspective it is

1) useless - count() provides no useful information about strings
2) potentially buggy.

For instance, PEAR's XML_Unserializer (XML_Serializer package)
represents XML elements with simple text node children as a string, and
tags with anything else (attributes, child elements) as an array.
Improperly written code that uses count() without checking is_array()
would happily run with no warning whatsoever. However, imho it would
make PHP more robust if the logical error condition were also a language
error, that's all.

Greg

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

#9 Oct. 27, 2005 11:11:03

Jani T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

[PHP-DEV] Re: foreach with null


On Wed, 26 Oct 2005, Ilia Alshanetsky wrote:Greg Beaver wrote:amen, this behavior makes no sense even though sizeof() is an alias to
count(). sizeof('this') and sizeof('this long thing') are both 1, which
makes no sense. I would go so far as to say a E_NOTICE is more
appropriate than E_STRICT - you should only be using count() for
arrays/objects.Actually this result makes perfect sense since type conversion changes
string into array('your string') and does a count of that, which is 1.This is yet another magic type casting I'd like to get rid of..
Strict is not always bad, mmkay? :)

I got bitten by this (unexpected?) behaviour of count() myself this week.
I must have been strict in my mind and not passed anything but arrays
to it before this. :)

--Jani

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit:http://www.php.net/unsub.php

Offline

  • Root
  • » PHP
  • » [PHP-DEV] Re: foreach with null [RSS Feed]

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support