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
Zig Roadmap 2021. From Zig SHOWTIME #21Subscribe to the Zig SHOWTIME Newsletter!https://zig.show0:00 Intro then Language Spec w/ Martin ...
New
First poster: bot
Kawa is a general-purpose programming language that runs on the Java platform. It aims to combine: the benefits of dynamic scripting la...
New
First poster: bot
Where is the Ruby language headed? At RubyConf 2021, the presentations about the language focused on static typing and performance—where ...
New
CommunityNews
By the end of this guide we’ll have a minimal, working implementation of a small part of Lua from scratch.
New
CommunityNews
Letting Go of Random. In a recent post I shared some thoughts about art and included a few, somewhat tongue-in-cheek comments about the ...
/go
New
First poster: adamaiken89
PHP: Frankenstein arrays. PHP has become quite a nice language, but there are some ugly legacies left from the past. Like the deceptive ...
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
First poster: bot
v4 Announcement · actix/actix-web Wiki. Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust. - v4 Announcement...
New
First poster: bot
GitHub - Shirakumo/trial: A fully-fledged Common Lisp game engine. A fully-fledged Common Lisp game engine. Contribute to Shirakumo/tria...
New
First poster: bot
Some Thoughts on Zig — Sympolymathesy, by Chris Krycho. One of the biggest things Zig has going for it—especially compared to Rust—is th...
New

Other popular topics Top

Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New