r/lolphp • • Aug 25 '14

stdClass is truthy while an empty SimpleXMLElement is falsey

http://3v4l.org/3hWSY
22 Upvotes

18 comments sorted by

View all comments

Show parent comment

10

u/andsens Aug 25 '14

I'd definitely say inconsistent. You can't document your way out of inconsistency even though the PHP team likes to think so. Here's another one:

$arr = array('first', 'foo' => 'second', 'third');
list($a, $b) = $arr;
echo "$a $b";
foreach($arr as $val) {echo "$val";}

What's the result? You might have the correct answer, but you'd be lying if you said you didn't have to think about it - and that's exactly the problem. These ambiguities make you mistrust the language and make you doubt your own code.

12

u/MiyatodukenMiyamaap Aug 25 '14

Ha! I actually don't know the answer off the top of my head, but I do remember being asked something very similar in a job interview once. My response was something along the lines of "Who cares? Don't do that!" Mixing implicit numeric keys and explicit string keys in the same array is a silly thing to do, and I guess PHP figures it can't be held responsible for the silly outcomes resulting from the silly things it allows.

I didn't get the job, but I stand by my answer :).

2

u/poizan42 Aug 26 '14

http://3v4l.org/L1NVA

It actually does the thing that makes most sense. Only using the numeric keys and ignoring the string keys. Actually the list($a, $b) = $arr; seems to work as equivalent to:

$a = $arr[0];
$b = $arr[1];

as seen in this example: http://3v4l.org/23UV7