Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

http://3v4l.org/GIn4Kt

The above code shows php silently converting numeric string keys into integer keys. This ends up breaking the concept of a dictionary and prevents it to be used as a proper dictionary.

Now if you want to use it as a set. This wont work http://3v4l.org/NZYsj

Also, These are the following are the functions that are supposed to be used when you want to use an array as a set.

array_​diff_​assoc

array_​diff_​key

array_​diff_​uassoc

array_​diff_​ukey

array_​diff

array_​intersect_​assoc

array_​intersect_​key

array_​intersect_​uassoc

array_​intersect_​ukey

array_​intersect

Consider the fact that arrays can have mixed numeric and string keys, and consider the fact that php converts numeric string keys into integer keys, and consider php weak comparison rules (2=='2'), and how do you feel about using the above functions for set operations?

You see, the php philosophy is to compensate a lack of depth with a whole lot of breadth. Instead of demanding the user to understand different data structures in depth, it provides one data structure that requires very little understanding to get started, and cover the drawbacks of with a number of functions, edge case rules, and undocumented weird behaviors (resulting from different combinations of documented, but unintuitive behaviors).

So the end result is you got away with only having to learn one simple data structure. But if you want to use it reliably, with the provided library functions, you need to keep an encyclopedic knowledge of the aforementioned edge cases gotchas. This is true with a lot of aspects of PHP. PHP is like a cheap-shitty-dangerous car that you can acquire with very little down payment on a loan with a huge interest.

>Javascript work similarly with one combined array/dict type.

Javascript have combined array/dict? How come?

>Also the PHP SPL exists..

It does not matter, People starting with php won't be aware of these pitfalls to go looking into SPL objects.



> Javascript have combined array/dict? How come?

Ha! Arrays in JS are just objects disguised as arrays, with a fancy prototype and some syntax sugar: no more, no less. There are no _real_ arrays in JS (in the traditional sense).

Keys in JS arrays are _strings_, weird as it is, because arrays are just objects, and objects use strings as keys. Try this yourself:

    for (var x in ['a']) console.log(x, typeof(x));
Specification may (or may not) clear things up:

http://www.ecma-international.org/ecma-262/5.1/#sec-15.4

Also you can try this out:

    ['a'][0] // = 'a'
    ['a']['0'] // = 'a'
    ['a']['00'] // = undefined
Numbers are coerced into strings, and not the other way around! (Note that I said 'number' on purpose, since integers are available but rarely used on the web, for BC. So it's usually floats coerced into strings every time you do ['a'][0]).

This weird coertions are not much unlike PHP's, which turns anything you give it as a key into a hash, internally. Though PHP arrays are far more complex, and even weirder (like how are they're not passed as a reference or as copy, but kinda both).


On the face of it you're technically correct, and i've been bitten by numeric string key coercion in the past. But another way of thinking about it is that PHP's combined array/dict implementation is a perfectly valid set/dict, just over a subset of values where 2 and "2" are not really distinct. And clearly this was intentional given all the other parts of the language that treat types weakly.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: