this post was submitted on 14 Jul 2023
330 points (90.8% liked)

Programmer Humor

32461 readers
797 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] kibiz0r@lemmy.world 158 points 1 year ago (15 children)

To be fair: If you are chaining ternary expressions, you deserve to suffer whatever pain the language happens to inflict upon you tenfold.

load more comments (15 replies)
[–] xedrak@kbin.social 57 points 1 year ago (2 children)

I get hating on PHP is a meme, and the language certainly has faults, but I feel like it’s no more arbitrary than how JavaScript behaves. And just like JavaScript, if you follow modern standards and use a modern version, it’s a much better experience. The language is only as good as the programmer.

[–] CanadaPlus@lemmy.sdf.org 48 points 1 year ago (1 children)

but I feel like it’s no more arbitrary than how JavaScript behave

This is not the flex you think it is.

[–] xedrak@kbin.social 21 points 1 year ago (5 children)

I didn’t mean it as a flex. It was a commentary on how the most commonly used programming language in current days is just as flawed as the most commonly used programming language in the past (in web development). Bad programmers are going to write bad code, regardless of the language.

load more comments (5 replies)
[–] Frools@lemm.ee 16 points 1 year ago (1 children)

That's why JS gets hates on just as much as PHP, if not more so these days as JS is fuckin everywhere!

load more comments (1 replies)
[–] cupcakezealot@lemmy.blahaj.zone 51 points 1 year ago (1 children)

The fault is the programmer for not using a switch statement.

[–] thatwill@lemmy.world 53 points 1 year ago (7 children)

"php doesn't stop me from coding like a moron, therefore php sucks"

[–] Araozu@lemmy.world 15 points 1 year ago (1 children)

How about "php enables me to code like a moron", or even better, "php breaks common conventions and forces me to think about every little detail and special edge case, slowing me down if I don't want to accidentally 'code like a moron' "

Nested ternary operators emerge because of the lack of if/switch expressions (which is C fault), so they are "useful" (they shouldn't be). However, PHP is the only language that treats it as left associative. This has 2 problems:

  • You are forced to use parenthesis. Some (insane) people might do: (cond1) ? "A" : (cond2) ? "B" : "C" And it makes sense. Its ugly af, but it makes sense. But PHP now forces you to use more parethesis. It's making you work more.
  • It breaks convention. If you come from any other language and use ternary operators, you will get unexpected results. After hours of banging your head against the wall, you realize the problem. And now you have to learn a new edge case in the language, and what to do to actually use the language.

"But you shouldn't use ternary operators anyway! Use if/switch/polymorphic dispatch/goto/anything else"

True, but still, the feature is there, and its bad. The fact that there are other alternatives doesn't make the PHP ternary operator worse than other languages' ternary operator.

PHP works against you. That's the problem. The ternary operator is not a good example, since there are alternatives. But look at something so simple, so mundane like strpos.

If strpos doesn't find returns false. Every other language returns -1. And if you then use this value elsewhere, PHP will cast it to 0 for you. Boom, your program is broken, and you have to stare at the screen for hours, looking for the error.

"BuT yOU sHoUlD AlwAyS cHEcK tHe rETurN eRRor!"

And even if that's true, if we all must check the return value, does PHP force you to do so? Like checked exceptions in Java? Or all the Option & Result in Rust? throws, throws, throws... unwrap, unwrap, unwrap... (Many) people hate those features

PHP works against you. And that's why its bad.

load more comments (1 replies)
[–] pazukaza@lemmy.ml 4 points 1 year ago

But if you code like a moron the code should still behave as expected. People who code like this deserve a special place in hell, next to languages that behave like that.

load more comments (5 replies)
[–] baascus@lemmy.world 36 points 1 year ago (3 children)

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 18 points 1 year ago (1 children)

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 13 points 1 year ago (1 children)

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 4 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) (1 children)

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

[–] mosiacmango@lemm.ee 12 points 1 year ago (2 children)

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

load more comments (2 replies)
[–] fsniper@kbin.social 3 points 1 year ago

it was never an array to begin with!

[–] asceticism@lemmy.fmhy.ml 36 points 1 year ago* (last edited 1 year ago) (1 children)

This is not valid syntax as of 2020. PHP 8 fixed a lot of issue like this as well as a lot of function and variable type issues.

Also this was deprecated in PHP 7 (2015).

[–] ezchili@iusearchlinux.fyi 10 points 1 year ago (1 children)

They deprecated nested ternaries?

[–] asceticism@lemmy.fmhy.ml 11 points 1 year ago* (last edited 1 year ago)

Not nested but 'Unparenthesized'

Also per the error message here is it working:

[–] kolorafa@lemmy.world 32 points 1 year ago

PHP Fatal error: Unparenthesized a ? b : c ? d : e is not supported. Use either (a ? b : c) ? d : e or a ? b : (c ? d : e)

[–] NorwegianBlues@sh.itjust.works 32 points 1 year ago (3 children)

Sure, it's counterintuitive, but so is not bracketing things in ternary operations.

[–] akariii@lemmy.blahaj.zone 5 points 1 year ago

that makes so much fucking sense

load more comments (2 replies)
[–] mikegioia@lemmy.ml 31 points 1 year ago (18 children)

Hating on php is one of the reasons i left reddit. This is just people who don’t use php hating php for some reason. You can do dumb examples like this for any language. Low effort and funny for children.

[–] deaf_fish@lemm.ee 8 points 1 year ago (3 children)

Your feelings are valid. I wonder though, would you put up this level of defense for posts making fun of arbitrary parts of non PHP languages?

You are not your favorite language. And I find most criticisms of most languages to be very valid. I don't think the intent of OP is to insult all PHP programmers. It's okay to like a language that has problems. All languages do.

[–] Doug@midwest.social 6 points 1 year ago

I'd wager prevalence is part of their problem. Jokes get tired after a while, but that doesn't always mean they stop.

PHP, like any language, has its problems, but it seems to get poked at a lot more often. But making the same joke over and over has been a problem long before reddit was a thing.

load more comments (2 replies)
[–] Heavybell@lemmy.world 5 points 1 year ago

Hey, I hate php AND javascript, and I've worked in both of them. :P

load more comments (16 replies)
[–] skullgiver@popplesburger.hilciferous.nl 31 points 1 year ago* (last edited 11 months ago)

[This comment has been deleted by an automated system]

[–] dot20@lemmy.world 28 points 1 year ago (1 children)

Ah, I understand now. The expression is evaluated like this:

  • $a == 1 ? "one" : $a == 2 ? "two" : $a == 3 ? "three" : "other"
  • $a == 2 ? "two" : $a == 3 ? "three" : "other"
  • "two" ? "three" : "other"
  • "three"
[–] lightsecond@programming.dev 10 points 1 year ago (1 children)

Halp. I don’t understand how it went from step 2 to step 3.

[–] sanguinet@lemmy.ca 26 points 1 year ago (3 children)

It's cause PHP associates the if-then-else pair only with its immediate "else" option, not with the entirety of the line.

Let's go by parts.

$a == 1 ? "one" : $a == 2 ? "two" : $a == 3 ? "three" : "other"

Is $a equal to 1? If so, we're "set" to the value on the left, which is "one", if not then we're set to the value on the right, which is $a == 2. $a is not equal to 1, so we're set to the right value, $a == 2.

This replaces the relevant part, $a == 1 ? "one" : $a == 2, with $a == 2. So we're left with:

$a == 2 ? "two" : $a == 3 ? "three" : "other"

Next, is $a equal to 2? If so, we're set to "two", if not we're set to $a == 3. The comparison is true, so we're set to the value on the left, "two". The relevant part here is $a == 2 ? "two" : $a == 3 only, so it replaces it with "two" cause again, PHP is only associating with its immediate pair. So now we're left with:

"two" ? "three" : "other"

Finally, is "two" truthy? If so, we're set to "three", if not we're set to "other". Since "two" is truthy we're then left with "three".

It's super confusing for sure.

[–] lugal@lemmy.one 4 points 1 year ago

Thanks! I never worked with PHP but I understand your explanation. Making memes about languages is also about learning

load more comments (2 replies)
[–] fubo@lemmy.world 18 points 1 year ago (1 children)
[–] AlmightySnoo@lemmy.world 8 points 1 year ago (2 children)
[–] dot20@lemmy.world 16 points 1 year ago (1 children)

Ah, I understand now. The expression is evaluated like this:

  • $a == 1 ? "one" : $a == 2 ? "two" : $a == 3 ? "three" : "other"
  • $a == 2 ? "two" : $a == 3 ? "three" : "other"
  • "two" ? "three" : "other"
  • "three"
load more comments (1 replies)
[–] fubo@lemmy.world 9 points 1 year ago* (last edited 1 year ago) (2 children)

If you think PHP is weird, go look up ZZT-OOP, the scripting language from Tim Sweeney's first game.

(No, a scripting language for game characters doesn't need integers. If you need to count, you can do that by moving blocks around on the game board. It's halfway between LOGO and Minecraft.)

load more comments (2 replies)
[–] Lemmypy@feddit.nl 17 points 1 year ago (1 children)

Finally got it...

$a == 1 ? "one" : ( ( $a == 2 ? "two" : $a == 3 ) ? "three" : "other" )

[–] nihilomaster@lemmy.ml 4 points 1 year ago (1 children)

because "two" is a truthy value?

load more comments (1 replies)
[–] Arotrios@kbin.social 12 points 1 year ago

Now do CGI.

Please. I worked with it for five years and I still don't understand it.

[–] hypertown@lemmy.world 10 points 1 year ago

!programmeranimemes@lemmy.world

[–] Mookulator@wirebase.org 5 points 1 year ago (2 children)
[–] BorgDrone@lemmy.one 23 points 1 year ago (1 children)

To quote the guy who invented PHP:

"I don't know how to stop it, there was never any intent to write a programming language [...] I have absolutely no idea how to write a programming language, I just kept adding the next logical step on the way."

[–] larvyde@sh.itjust.works 3 points 1 year ago

Yes, PHP was originally meant to be a template language, like handlebars or thymeleaf, but kept on accumulating features along the way...

load more comments (1 replies)
[–] onichama@feddit.de 3 points 1 year ago

Thanks I hate it.

load more comments
view more: next ›