334
submitted 1 year ago* (last edited 1 year ago) by AlmightySnoo@lemmy.world to c/programmerhumor@lemmy.ml
you are viewing a single comment's thread
view the rest of the comments
[-] baascus@lemmy.world 35 points 1 year ago

Ever wondered about the array_fill function? It can be baffling. Try filling an array with a negative index:

array_fill(-5, 4, 'test');

Many languages would throw an error, but PHP? It’s perfectly fine with this and you get an array starting at index -5. It’s like PHP is the Wild West of array indexing!

[-] marcos@lemmy.world 19 points 1 year ago

Well, many languages are perfectly ok with negative array indexes.

But all of those languages are either statically typed ones where you declare the boundings with the array, or php.

[-] baascus@lemmy.world 14 points 1 year ago

Absolutely, many languages do allow negative indices. The intriguing part about PHP, though, is that its ‘arrays’ are actually ordered maps (or hash tables) under the hood. This structure allows for a broader range of keys, like our negative integers or even strings. It’s a unique design choice that sets PHP apart and allows for some really interesting usage patterns. Not your everyday array, right?

[-] Funwayguy@lemmy.world 5 points 1 year ago

I've been working with PHP for two years now (not by choice) but I still sometimes forget the weird behaviours these not-arrays cause. Recently I was pushing/popping entries in a queue and it fucked the indexing. I had programmed it like I would any other sane language and it wasn't until I was stepping through the bug I realised I had forgotten about this.

I hate PHP for so many more reasons. It baffles me why anyone would think it was a good idea to design it this way. Thankfully my current job involves actively burning it down and preparing for its replacement.

[-] Windex007@lemmy.world 12 points 1 year ago* (last edited 1 year ago)

You think that's bad, just wait until you hear what C does

[-] mosiacmango@lemm.ee 12 points 1 year ago

"Shot myself in the foot? No, no. I took out the whole leg."

[-] exu@feditown.com 1 points 1 year ago

That's C++ you're thinking off.

[-] rikudou@lemmings.world 1 points 1 year ago

Yeah, because casting everything to void* is so safe.

[-] fsniper@kbin.social 3 points 1 year ago

it was never an array to begin with!

this post was submitted on 14 Jul 2023
334 points (90.7% liked)

Programmer Humor

32371 readers
597 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS