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.
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.
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.
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.
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.
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.
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.
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 get left out unintentionally?”
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.
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.
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!
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?
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.
Yeah I have a Samsung phone and have never had this problem so I’m guessing it’s carrier stuff and op is unkowongly placing blame on the wrong company here
I wish manufacturers didn’t allow carriers to install junk on their phone.
On the other hand, this is how carriers can give you a good deal on a phone… They have to subsidise it by making deals with the companies that make apps like these.
If they have to trick you by installing unwanted apps on your phone, where you don’t even know it’s them doing it, then it sounds more like a scam than a great deal.
I agree. That’s unfortunately how it is today though - either you pay full price for an unlocked phone (which some people can’t afford), or you get junk apps on it. You can usually disable the junk apps at least.
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”.
Hey; if you’re interested in this, I recommend Nevermind. It’s a game that gets harder when you get scared. It uses a heart rate monitor or your webcam. It’s supposed to teach you to make yourself calm down.
“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.”
In the kind of tale any aspiring BOFH would be able to dine out on for months, the University of North Carolina has finally located one of its most reliable servers - which nobody had seen for FOUR years.
One of the university's Novell servers had been doing the business for years and nobody stopped to wonder where it was - until some bright spark realised an audit of the campus network was well overdue.
According to a report by Techweb it was only then that those campus techies realised they couldn't find the server. Attempts to follow network cabling to find the missing box led to the discovery that maintenance workers had sealed the server behind a wall.
lemmy.world
Newest