CommunityNews

CommunityNews

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.

Most Liked

dimitarvp

dimitarvp

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.

mindriot

mindriot

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

mindriot

mindriot

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).

Where Next?

Popular Backend topics Top

First poster: bot
A conversation with Laurent Mazare about how your choice of programming language interacts with the kind of work you do, and in particula...
New
CommunityNews
Algebraic effects and handlers provide a modular abstraction for expressing effectful computation, allowing the programmer to separate th...
New
First poster: bot
Why Lisp? A lot of people ask us the question, why do we choose to use Common Lisp as our primary development language? Often times the q...
New
First poster: bot
50 Years of Prolog and Beyond. Both logic programming in general, and Prolog in particular, have a long and fascinating history, interm...
New
First poster: bot
GitHub - vitalik/django-ninja: :dash: Fast, Async-ready, Openapi, type hints based framework for building APIs. :dash: Fast, Async-rea...
New
New
First poster: bot
GitHub - codic12/worm: A dynamic, tag-based window manager written in Nim. A dynamic, tag-based window manager written in Nim - GitHub -...
New
First poster: faust
Ruffle is a Flash Player emulator written in Rust. Ruffle runs natively on all modern operating systems as a standalone application, and ...
New
First poster: herbert
Why Rust should not have provided unwrap. I see the unwrap function called a lot, especially in example code, quick-and-dirty prototype ...
New
First poster: bot
user-defined iteration using range over func values · Discussion #56413 · golang/go. There is no standard way to iterate over a sequence...
/go
New

Other popular topics Top

DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New