and people wonder why we say PHP is a meme

3 years ago by AlmightySnoo 🐢🇮🇱🇺🇦 to c/programmerhumor

kibiz0r 175 points 3 years ago

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

path: 0 1241156, hotness: undefined, score: 175, children: 15
Serdan -11 points 3 years ago

Why?

It's perfectly readable.

path: 0 1241156 1246427, hotness: undefined, score: -11, children: 14
lowleveldata 38 points 3 years ago

It is sort of readable. A switch is "perfectly" readable for switching.

path: 0 1241156 1246427 1248624, hotness: undefined, score: 38, children: 7
rikudou 3 points 3 years ago

Match is even better, short and sweet.

path: 0 1241156 1246427 1248624 1315320, hotness: undefined, score: 3, children: 0
Serdan -5 points 3 years ago

Ternary expressions aren't switches though

path: 0 1241156 1246427 1248624 1263557, hotness: undefined, score: -5, children: 5
Kryomaani 18 points 3 years ago

Which is exactly why you shouldn't be using them in a situation that clearly calls for a switch.

path: 0 1241156 1246427 1248624 1263557 1271127, hotness: undefined, score: 18, children: 4
deaf_fish 0 points 3 years ago

There is usually a safer and more readable way to do what you want to do by chaining ternaries in most languages.

path: 0 1241156 1246427 1261901, hotness: undefined, score: 0, children: 5
Serdan 4 points 3 years ago

How is it unsafe?

path: 0 1241156 1246427 1261901 1275488, hotness: undefined, score: 4, children: 4
deaf_fish 1 point 3 years ago

Well, if you assume ternary operations work the same in PHP as in c and attempted to write the code demoed by this meme. You would end up with unexpected behavior. Maybe I should have said unexpected behavior instead of unsafe behavior.

path: 0 1241156 1246427 1261901 1275488 1286540, hotness: undefined, score: 1, children: 3
xedrak 58 points 3 years ago

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.

path: 0 1241334, hotness: undefined, score: 58, children: 9
CanadaPlus 49 points 3 years ago

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

This is not the flex you think it is.

path: 0 1241334 1245303, hotness: undefined, score: 49, children: 6
xedrak 22 points 3 years ago

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.

path: 0 1241334 1245303 1246288, hotness: undefined, score: 22, children: 5
Funwayguy 3 points 3 years ago

I'd like to think Typescript does a lot of heavy lifting where JS fails when it comes to web development. On the otherhand there is no fixing fundamental flaws in PHP.

Sure bad programmers write bad code, but if a language tolerates something so obviously janky via implicit unseen magic, it's just encouraging bad practices. PHP makes this worse by tweaking core behaviours in weird and wacky ways that can easily lead to security vulnerabilities.

path: 0 1241334 1245303 1246288 1247445, hotness: undefined, score: 3, children: 1
CanadaPlus 2 points 3 years ago

A high level language should make coding easier. If it doesn't why not just code in assembly, or even build an ASIC? And no, allowing you to fuck yourself by accident isn't making your life easier even if it seems simpler at the time you write the code.

path: 0 1241334 1245303 1246288 1247445 1265122, hotness: undefined, score: 2, children: 0
CanadaPlus 1 point 3 years ago

Why not just use a good language though? Most of them are decent. It sounds like typescript is getting used a lot, at least - although it then gets turned into JavaScript for the browser.

path: 0 1241334 1245303 1246288 1265240, hotness: undefined, score: 1, children: 2
zalgotext 3 points 3 years ago

Why not just use a good language though?

You don't really have a choice when it comes to front end web dev, JS is almost the only option for programming languages that run in the browser.

path: 0 1241334 1245303 1246288 1265240 1279449, hotness: undefined, score: 3, children: 1
Frools 16 points 3 years ago

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

path: 0 1241334 1243447, hotness: undefined, score: 16, children: 1
cupcakezealot 1 point 3 years ago

JS is just popular because Google invests so much money into it to use it to make the web worse off.

path: 0 1241334 1243447 1262379, hotness: undefined, score: 1, children: 0
cupcakezealot 53 points 3 years ago

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

path: 0 1262279, hotness: undefined, score: 53, children: 9
thatwill 56 points 3 years ago

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

path: 0 1262279 1266737, hotness: undefined, score: 56, children: 8
Araozu 15 points 3 years ago

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.

path: 0 1262279 1266737 1299009, hotness: undefined, score: 15, children: 1
rikudou 2 points 3 years ago

Show us on this doll where php hurt you. Php is great, especially in the later versions. I rarely need to know the position of a substring in a string, most of the time str_starts_with(), str_ends_with() and str_contains() is what I really need.

path: 0 1262279 1266737 1299009 1315246, hotness: undefined, score: 2, children: 0
pazukaza 4 points 3 years 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.

path: 0 1262279 1266737 1280199, hotness: undefined, score: 4, children: 0
bastian_5 4 points 3 years ago

I say that php breaks math entirely, and is therefore bad. "" == null returns true null == [] returns true "" == [] returns false.

In more recent versions it gets worse, because it has 0 == "any text" return true, "any text" == true return true, and 1 == true return true So indirectly 1 = 0, and now math is more directly broken.

path: 0 1262279 1266737 1269124, hotness: undefined, score: 4, children: 4
thatwill 13 points 3 years ago

If you're trying to directly compare different variable types in any language without strong typing, you're going to have edge-case results which you might not expect.

My "coding like a moron" message still stands. PHP isn't a strongly typed language and it doesn't tell you off for trying stupid stuff like comparing a string with an int. Nor do other languages like JavaScript.

path: 0 1262279 1266737 1269124 1274598, hotness: undefined, score: 13, children: 1
kimpilled 4 points 3 years ago

Also using duck typing fails against php is pretty funny when it’s being compared against JAVASCRIPT of all things.

path: 0 1262279 1266737 1269124 1274598 1275612, hotness: undefined, score: 4, children: 0
thatwill 2 points 3 years ago

I just tested these out out of curiosity.
0=="text" returns false in PHP 8.2 as I'd expect.

The others make sense in the way that php juggles between types. An empty variable can type-juggle to null, but an array can't be directly compared with a string.

(Although you wouldn't really want to compare an array with a string, PHP just treats an array as greater than other variables. So weirdly, ([] > "") == true.)

path: 0 1262279 1266737 1269124 1276925, hotness: undefined, score: 2, children: 0
rikudou 1 point 3 years ago

Just... don't use ==? I haven't used it in a few years.

path: 0 1262279 1266737 1269124 1315277, hotness: undefined, score: 1, children: 0
baascus 38 points 3 years 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!

path: 0 1241147, hotness: undefined, score: 38, children: 8
marcos 21 points 3 years 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.

path: 0 1241147 1241281, hotness: undefined, score: 21, children: 2
baascus 15 points 3 years 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?

path: 0 1241147 1241281 1241382, hotness: undefined, score: 15, children: 1
Funwayguy 5 points 3 years 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.

path: 0 1241147 1241281 1241382 1247209, hotness: undefined, score: 5, children: 0
Windex007 12 points 3 years ago

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

path: 0 1241147 1242243, hotness: undefined, score: 12, children: 3
mosiacmango 12 points 3 years ago

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

path: 0 1241147 1242243 1242989, hotness: undefined, score: 12, children: 2
exu 1 point 3 years ago

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

path: 0 1241147 1242243 1242989 1251453, hotness: undefined, score: 1, children: 1
rikudou 1 point 3 years ago

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

path: 0 1241147 1242243 1242989 1251453 1315517, hotness: undefined, score: 1, children: 0
fsniper 3 points 3 years ago

it was never an array to begin with!

path: 0 1241147 1247274, hotness: undefined, score: 3, children: 0
asceticism 36 points 3 years ago

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

path: 0 1256201, hotness: undefined, score: 36, children: 2
ezchili 10 points 3 years ago

They deprecated nested ternaries?

path: 0 1256201 1260051, hotness: undefined, score: 10, children: 1
asceticism 11 points 3 years ago

Not nested but 'Unparenthesized'

Also per the error message here is it working:

path: 0 1256201 1260051 1281039, hotness: undefined, score: 11, children: 0
NorwegianBlues 34 points 3 years ago

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

path: 0 1249396, hotness: undefined, score: 34, children: 3
akariii 5 points 3 years ago

that makes so much fucking sense

path: 0 1249396 1254668, hotness: undefined, score: 5, children: 0
ezchili 1 point 3 years ago

You know that programmers of other languages don't have to find excuses for their tool constantly, right?

path: 0 1249396 1263371, hotness: undefined, score: 1, children: 1
rikudou 2 points 3 years ago

You don't know many languages, huh?

path: 0 1249396 1263371 1315449, hotness: undefined, score: 2, children: 0
kolorafa 34 points 3 years 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)

path: 0 1271920, hotness: undefined, score: 34, children: 0
skullgiver 32 points 3 years ago
path: 0 1261739, hotness: undefined, score: 32, children: 0
mikegioia 31 points 3 years ago

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.

path: 0 1250912, hotness: undefined, score: 31, children: 21
deaf_fish 8 points 3 years ago

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.

path: 0 1250912 1261696, hotness: undefined, score: 8, children: 3
Doug 6 points 3 years 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.

path: 0 1250912 1261696 1279825, hotness: undefined, score: 6, children: 0
mikegioia 1 point 3 years ago

PHP gets a totally disproportionate share of hate and that is the problem. Children like to dunk on PHP and a group mentality pushes it even more.

path: 0 1250912 1261696 1296195, hotness: undefined, score: 1, children: 1
deaf_fish -1 points 3 years ago

Maybe it does. Maybe it doesn't. This is the first post I have seen pointing out a flaw in PHP on Reddit or Lemmy. If you ask me JavaScript gets it the worst out of all of the languages. I don't see those guys whining.

What I will say is that the majority of PHP developers I interact with on this post has led me to believe that there is a number of PHP developers that take things way to personally that they really shouldn't. Seriously you guys aren't doing your language any favors. No one's going to want to join the whine club.

path: 0 1250912 1261696 1296195 1313145, hotness: undefined, score: -1, children: 0
Heavybell 5 points 3 years ago

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

path: 0 1250912 1260367, hotness: undefined, score: 5, children: 0
BinarySystem 3 points 3 years ago

I took this more as a light-hearted poke at a silly edge case. As someone who used to build static analysis software for various languages, including PHP. This gets a chuckle out of me as it takes me back to having to deal with these exact types of edge cases.

path: 0 1250912 1272847, hotness: undefined, score: 3, children: 1
mikegioia 1 point 3 years ago

The title of this post is calling php a “meme” did you read that part? Then it just uses some stupid ternary example no one does and that other languages exhibit so what is the point other than purely hating on php?

path: 0 1250912 1272847 1296404, hotness: undefined, score: 1, children: 0
ezchili 2 points 3 years ago

I used php for 2 years and I hate on it it's not just people who've never used it

path: 0 1250912 1259152, hotness: undefined, score: 2, children: 13
mikegioia 1 point 3 years ago

no clue what your point is.

path: 0 1250912 1259152 1296304, hotness: undefined, score: 1, children: 12
nailbar 4 points 3 years ago

The comment above claimed only people who never used PHP hate on it. The point was a counter claim to that.

path: 0 1250912 1259152 1296304 1296862, hotness: undefined, score: 4, children: 11
mikegioia -2 points 3 years ago

Ok I can add another group to my list then: bad php developers

path: 0 1250912 1259152 1296304 1296862 1297019, hotness: undefined, score: -2, children: 10
dot20 29 points 3 years ago
path: 0 1240497, hotness: undefined, score: 29, children: 5
lightsecond 11 points 3 years ago

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

path: 0 1240497 1247382, hotness: undefined, score: 11, children: 4
sanguinet 33 points 3 years ago

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.

path: 0 1240497 1247382 1248337, hotness: undefined, score: 33, children: 3
zarp86 5 points 3 years ago

Thank you for the great explanation, and for teaching me the word "truthy."

path: 0 1240497 1247382 1248337 1250499, hotness: undefined, score: 5, children: 0
lugal 5 points 3 years ago

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

path: 0 1240497 1247382 1248337 1253744, hotness: undefined, score: 5, children: 0
lightsecond 3 points 3 years ago

Thank you!

path: 0 1240497 1247382 1248337 1252301, hotness: undefined, score: 3, children: 0
Lemmypy 18 points 3 years ago

Finally got it...

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

path: 0 1241368, hotness: undefined, score: 18, children: 2
nihilomaster 4 points 3 years ago

because "two" is a truthy value?

path: 0 1241368 1245533, hotness: undefined, score: 4, children: 1
Worthstream 1 point 3 years ago

Yep, any non empty string is truthy.

path: 0 1241368 1245533 1294072, hotness: undefined, score: 1, children: 0
fubo 18 points 3 years ago

Wat.

path: 0 1239797, hotness: undefined, score: 18, children: 6
AlmightySnoo 8 points 3 years ago path: 0 1239797 1239922, hotness: undefined, score: 8, children: 5
dot20 19 points 3 years ago

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"
path: 0 1239797 1239922 1240516, hotness: undefined, score: 19, children: 1
lascapi 2 points 3 years ago
path: 0 1239797 1239922 1240516 1294716, hotness: undefined, score: 2, children: 0
fubo 11 points 3 years ago

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

path: 0 1239797 1239922 1239951, hotness: undefined, score: 11, children: 2
proflovski 1 point 3 years ago

I would expect this from Javascript :S

path: 0 1239797 1239922 1239951 1280934, hotness: undefined, score: 1, children: 1
fubo 2 points 3 years ago

ZZT came out in 1991. JavaScript didn't exist for at least four years after that.

path: 0 1239797 1239922 1239951 1280934 1280950, hotness: undefined, score: 2, children: 0
Arotrios 12 points 3 years ago

Now do CGI.

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

path: 0 1245262, hotness: undefined, score: 12, children: 0
hypertown 10 points 3 years ago path: 0 1280283, hotness: undefined, score: 10, children: 0
Mookulator 5 points 3 years ago

Why

path: 0 1240304, hotness: undefined, score: 5, children: 3
BorgDrone 25 points 3 years ago

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

path: 0 1240304 1242060, hotness: undefined, score: 25, children: 1
larvyde 4 points 3 years ago

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

path: 0 1240304 1242060 1248825, hotness: undefined, score: 4, children: 0
ezchili 1 point 3 years ago

They didn't think about which order ternaries should compute in when they coded the php interpreter

path: 0 1240304 1259231, hotness: undefined, score: 1, children: 0
onichama 3 points 3 years ago

Thanks I hate it.

path: 0 1248880, hotness: undefined, score: 3, children: 0
programmerhumor
programmerhumor

@lemmy.ml

login for more options
43772
1628
1567

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

go to feed...