PHP: Frankenstein arrays

PHP: Frankenstein arrays.
PHP has become quite a nice language, but there are some ugly legacies left from the past. Like the deceptive Frankenstein abomination known as the “array”.

Read in full here:

This thread was posted by one of our members via one of our news source trackers.

2 Likes

Corresponding tweet for this thread:

Share link for this tweet.

1 Like

This seems to rear its ugly head pretty often when having to deal with a JSON API. Had to deal with this a few times recently. Discovered the other week, if you give it a JSON object with only numeric keys (which are strings with numbers in them because numbers as keys are not valid JSON) regardless of whether they are sequential, it thinks the object is an array. Who would have thought {“1”; “one”, “9”: “nine”} should be turned into [“one”, “nine”]? :man_shrugging: apparently someone

3 Likes

PHP has some very interesting oddities, but is it more or less than JS’s very weird oddities, lol…

2 Likes

I’m not sure if it is more or less, JS weirdness I’m much more familiar with which doesn’t make it particularly easy to look at objectively. I do tend to find that while JS has a lot that is really easy to poke fun at, it is also generally fairly easy to avoid (within the bounds of a complete lack of type safety) many of the pitfalls. I also generally find a lot of discussion, examples of usage etc go a long way to pushing you away from those pitfalls as well. Laravel by default decodes JSON in this way, so with what is one of the most popular web frameworks you are basically being guided towards having this problem and certainly on the versions I’ve had to deal with recently (which are old) there is no easy way to get at the original request in the middleware chain or the validation rules they have, making things impacted by this decision painful to do validation on. Also today I learned in another case of PHP weirdness that the string “0” is falsey, so

if(“0”) {
    // do nothing
} else { 
    echo “wat!”; 
}

Does indeed echo “wat!”. Even JS isn’t quite that insane unless you introduce some coercion into the condition (at which point you will receive all the pain and suffering that comes with doing something so stupid).

3 Likes

The issues of PHP were because there was a standard as C wrapper and PHP own standards.
Also, the ways of patching PHP in the early versions…

JS is a totally different case. In the early years, each browser implements its own feature without comparing the style…

2 Likes

Oh I don’t even mean the exceptionally odd and inconsistent standard library functions, I mean as a language, most of the issues coming down to annoying dynamic typing.

2 Likes

PHP: the gift that keeps on giving.

And to think there are people furiously defending it to this day…

3 Likes

Today I read some of this comment thread on hacker news, boggles the mind PHP – The Right Way | Hacker News

3 Likes

To be fair, I lost patience reading such threads. They usually boil down to:

“If you are a disciplined hardcore veteran like myself you’ll never make these mistakes!”

Which is so dumb and unrealistic that it should not even be graced with the honor of replying to it.

4 Likes

There is a bit of that, there are a bunch of different points from different perspectives in there, but there are a surprising amount of people attempting to make serious arguments that PHP is a better choice than python or ruby, or generally most dynamic languages

3 Likes

Sunk cost fallacy. :person_shrugging:

I was 40 years old when I realized, for the first time in my life, that me willing to throw away acquired skills and moving to gain new ones, is an aberration and not the norm.

Most people learn a few things and hold onto them with a death grip.

I actually met a woman last year who was LOUDLY BRAGGING about knowing how to manage a Blogger page/blog and was asking me how can she reuse that knowledge if she were to learn how to do so for WordPress and Magento websites… A valid question, surely, but she was just hell-bent on learning the minimum possible amount and that really stood out to me.

3 Likes

Sounds like the reason some people to use C over Rust… >.>

The point of the extra features rust has isn’t for convenience in immediate programming, it’s to catch errors that EVERYONE, including the “grizzled veteran” (even if less often) makes.

PHP is on the level of C, it’s very low level, easy to break, no good early catches, etc…

3 Likes

The one that gets me is Go, it is creating something new that is an active rejection of everything anyone has learned since C. Though that doesn’t really relate to PHP at all, it does seem to suffer a lot from the idea that if you just do it right then it won’t be a total disaster.

3 Likes

Well, don’t forget the fact that one of Golang’s creators said they were aiming at fresh college grads with C or Java background…

2 Likes

I think the creator of Go had this to say about it:

The key point here is that our programmers are not researchers. They are, as a rule, very young, they come to us after their studies, perhaps they studied Java, C / C ++ or Python. They can’t understand an outstanding language, but at the same time we want them to create good software. That is why their language should be easy for them to understand and learn.

It should be familiar, roughly similar to C. Programmers working at Google start their careers early and are mostly familiar with procedural languages, in particular the C family. The demand for speedy productivity in a new programming language means that the language doesn’t have to be too radical.

I still don’t see how golang helps make good software, I keep running into crashes with go programs and libraries… >.>

2 Likes

Yep. And that was Google’s goal: a fresh stream of new grads to hire and use and throw away any time they need them.

Gotta admire their criminal genius. :slight_smile:

2 Likes

I totally agree with this, but would also add it should be intuitive and feel natural (so not frustrating) - these are some of the things that makes people want to use a language (some of the others are what you can do with it or the power it affords you) :smiley:

1 Like