!programmeranimemes@lemmy.world

Skull giver
link
fedilink
24
edit-2
1Y

[This comment has been deleted by an automated system]

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

Why?

It’s perfectly readable.

deaf_fish
link
fedilink
-11Y

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

How is it unsafe?

deaf_fish
link
fedilink
1
edit-2
1Y

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.

PHP is the only language in existence with a left associative ternary operator. Ignoring PHP, the operator has worked exactly the same way for decades. And even PHP has now fixed the operator.

I don’t think it’s reasonable to avoid a very commonly supported pattern just because a single badly designed language implemented it wrong.

deaf_fish
link
fedilink
11Y

Okay, even if I give you the unexpected behavior point. The readability problem remains. Switch statements or tables will work just fine and are easier to read.

To be clear, I am fine with single ternary operations. I think nested ternary operations are harder to read and follow.

I agree you should use a switch where applicable, but ternaries are the expression equivalent of if-else statements. If I have two conditions and a default, and each branch simply evaluates to a value of the same type, I’ll probably just use a ternary.

It is sort of readable. A switch is “perfectly” readable for switching.

Ternary expressions aren’t switches though

@Kryomaani@sopuli.xyz
link
fedilink
English
161Y

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

@Serdan@lemm.ee
link
fedilink
English
2
edit-2
1Y

In the given example I’d probably use a switch / match expression, but ternaries are usually more flexible than switches and I don’t think it’s an issue to write a nested ternary instead of if else statements.

@lowleveldata@programming.dev
link
fedilink
English
2
edit-2
1Y

ternaries are usually more flexible than switches

Which is bad for readability because the reader need to manually compute it to see whether it’s doing simple switching or not. Also it adds the question of “Why did the author use a nested ternary instead of a switch? Was it meant to do more but it got left out unintentionally?”

@Serdan@lemm.ee
link
fedilink
English
11Y

Yes, you need to read code to understand it. If else statements can also do the job of a switch, so the exact same argument applies.

Rikudou_Sage
link
fedilink
31Y

Match is even better, short and sweet.

finn
link
fedilink
41Y

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!

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.

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

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

exu
link
fedilink
01Y

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

Rikudou_Sage
link
fedilink
11Y

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

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

They deprecated nested ternaries?

Not nested but ‘Unparenthesized’

Also per the error message here is it working:

@kolorafa@lemmy.world
link
fedilink
English
241Y

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)

katy ✨
link
fedilink
411Y

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

“php doesn’t stop me from coding like a moron, therefore php sucks”

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.

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.

Rikudou_Sage
link
fedilink
31Y

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.

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.

Rikudou_Sage
link
fedilink
11Y

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

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

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.

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

mookulator
link
fedilink
11Y

Why

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
link
fedilink
11Y

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

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

akari
link
fedilink
31Y

that makes so much fucking sense

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

Rikudou_Sage
link
fedilink
21Y

You don’t know many languages, huh?

Finally got it…

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

because “two” is a truthy value?

Yep, any non empty string is truthy.

Wat.

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"
🌈 Lascapi
link
fedilink
11Y

@AlmightySnoo @dot20 hooooo 🤯!! Thank you for the solution! 🤓

@fubo@lemmy.world
link
fedilink
3
edit-2
1Y

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

I would expect this from Javascript :S

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

Mike
link
fedilink
181Y

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.

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

Mike
link
fedilink
21Y

no clue what your point is.

Nailbar
link
fedilink
41Y

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

Mike
link
fedilink
-31Y

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

Good developers don’t get tribal about the tools they use

It makes them able to switch away from them for better ones

I use typescript now

Mike
link
fedilink
-11Y

deleted by creator

Mike
link
fedilink
-31Y

This entire thread is a tribal post that I had a problem with!! You don’t seem to understand this but I don’t care.

Hard sell. Calling people you know nothing about “bad developers” because they don’t like your tool on the other hand, that was cringe as hell and just made you look like you somehow tied your self-worth to php. “If my tool gets criticized; that means I’m being criticized!”

If you don’t give in to that, you start to see “Oh, that bug wouldn’t have happened if I’d been using [x]” and you become a better developer

When a woodworker cuts iself with a bandsaw, people who do what you do scream “He’s a bad woodworker!”

And while they’re screaming, we invented guardrails

Pointing out what sucks in a language isn’t tribalism, calling people “bad developers” when they don’t like your tool on the other hand, …

level 1: “this argument is worthless because you are stupid!”

level 300: “everyone who hates php is just an idiot tbh”

Just standard discourse from the php community

Mike
link
fedilink
21Y

There are reasons to dislike a language, but there are no reasons to hate a language, certainly not one that is as ubiquitous as PHP. There’s no argument you could make for why someone hates a programming language.

Introducing bugs into code way more often than others with similarly skilled programmers is one good reason to hate it…

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.

Mike
link
fedilink
11Y

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?

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

deaf_fish
link
fedilink
21Y

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.

Mike
link
fedilink
11Y

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.

deaf_fish
link
fedilink
-11Y

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.

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.

Arotrios
link
fedilink
21Y

Now do CGI.

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

xedrak
link
fedilink
141Y

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.

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

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

This is not the flex you think it is.

xedrak
link
fedilink
31Y

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.

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.

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.

Yes, there’s a lot of lock-in with JavaScript. The first step would be designing a Python-based browser or whatever and getting people to use it. I know less about PHP.

That was more of a “why if you had the option”. I’ve use JavaScript too for exactly this reason.

Funwayguy
link
fedilink
01Y

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.

Create a post

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.
  • 1 user online
  • 77 users / day
  • 211 users / week
  • 413 users / month
  • 2.92K users / 6 months
  • 1 subscriber
  • 1.53K Posts
  • 33.8K Comments
  • Modlog