160 points

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

permalink
report
reply
-10 points

Why?

It’s perfectly readable.

permalink
report
parent
reply
36 points

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

permalink
report
parent
reply
3 points

Match is even better, short and sweet.

permalink
report
parent
reply
-5 points

Ternary expressions aren’t switches though

permalink
report
parent
reply
0 points

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

permalink
report
parent
reply
4 points

How is it unsafe?

permalink
report
parent
reply
57 points

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.

permalink
report
reply
51 points

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

This is not the flex you think it is.

permalink
report
parent
reply
21 points

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.

permalink
report
parent
reply
3 points

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.

permalink
report
parent
reply
0 points
*

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.

permalink
report
parent
reply
17 points

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

permalink
report
parent
reply
1 point

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

permalink
report
parent
reply
51 points

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

permalink
report
reply
53 points

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

permalink
report
parent
reply
15 points

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.

permalink
report
parent
reply
2 points

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.

permalink
report
parent
reply
4 points

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.

permalink
report
parent
reply
3 points

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.

permalink
report
parent
reply
13 points

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.

permalink
report
parent
reply
2 points

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

permalink
report
parent
reply
1 point

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

permalink
report
parent
reply
36 points
*

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

permalink
report
reply
10 points

They deprecated nested ternaries?

permalink
report
parent
reply
11 points
*

Not nested but ‘Unparenthesized’

Also per the error message here is it working:

permalink
report
parent
reply
36 points

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!

permalink
report
reply
19 points

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.

permalink
report
parent
reply
14 points

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?

permalink
report
parent
reply
5 points

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.

permalink
report
parent
reply
12 points
*

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

permalink
report
parent
reply
13 points

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

permalink
report
parent
reply
1 point

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

permalink
report
parent
reply
3 points

it was never an array to begin with!

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

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

Community stats

  • 6.4K

    Monthly active users

  • 1.5K

    Posts

  • 35K

    Comments