- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
This gave me an idea. Instead of saying things, I'll first record myself saying them with a voice recorder, and then I can play the recording. Genius.
Admin
"What is this wacko PHP syntax?? I'll just do what I do in Python, no one will notice, I'll work here for 40 years and this code'll be replaced in 5."
Admin
And then, if you need to pay attention to the things you're saying, you can play them into a telephone, and listen to it on the other end.
Admin
If C lets you shoot your foot off, and C++ is a foot machine gun, PhP is one of those guns with 50 different buttons in an alien movie that no one understands what all the buttons do.
Admin
Not even the aliens themselves kek
Admin
Huh? The array is filled with all 1's, there's no relationship between the two values. I think Remy confused this with
range(0, 100, 1)
Admin
TRWTF: PHP
Admin
Why is it valid syntax to use a variable reference in the parameters of a foreach loop? And who in any mind (right or not) would attempt it?
Admin
Unfortunately, it will be the other way around. You will be out of here in 5 years, but this code will still be in production in 40 years.
Admin
I was half-expecting an off-by-one error.
Admin
The foreach loop is considered to be much better in performance to that of the generic for loop. The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively
https://www.geeksforgeeks.org/performance-of-for-vs-foreach-in-php/#:~:text=The%20foreach%20loop%20is%20considered,loop%20in%20less%20time%20comparatively.
Admin
I read this page. Their performance test is terrible.
// for loop performance evaluation for($i = 0; $i < count($units); $i++) { }
Instead of using a constant, or a variable, they call the count function on each iteration.
WTF, indeed.
Admin
And we're arguing about performance in PHP now? Good grief.
(Obviously there are the same performance issues if you use an O(n^3) algorithm, as with any other language. And no doubt there's a bunch of PHP libraries out there that do horrible inefficient things with matrices. But, really?)
PHP isn't designed for performance-critical platforms. PHP isn't designed for coherent expression of ideas via syntax (cf Python).
PHP isn't designed.
Admin
Fairly ugly, but easily understandable once you "break the code" and trivial to replace. Show me someone who's trying to emulate a while loop or is actively extending the array by sneakily adjusting their iterable expression in a foreach loop and I'll show you a WTFTU (thumbs up).
Admin
Isn't the code doing double duty though? The array has to be populated with consecutive values & then iterated over, so isn't that more or less the equivalent of two for-loops?
Admin
Hey I didn't start this argument, merely correcting somebody else's misunderstanding. Even in PHP, there are better and worse ways to do things.
Admin
Do they though...? https://www.php.net/manual/en/function.array-fill.php - unless I'm reading this completely wrong, this array will be populated with just 1s. Which should explain why the variable it gets saved to in the second version is called $one.
Admin
Of course we do. They all shoot you in the foot. Or the arm. Or the face. Or teleport an angry rabid human hating egg laying duck-bever hybrid into your genitals. And while some of them appear to not do any of those things that's only because they are setting things up for later.
Admin
It's easy to see that a literalist interpretation of what
count
does (count the elements) would lead to the algorithm being unexpectedly O(n-squared) instead of O(n), but is that whatcount
does? Or does it just retrieve the number of elements from the array "object"?(Yeah, I know, it's PHP, so we should assume the worst interpretation, but ...) (Yeah, I've read eevee's fractal thing.)
Admin
Oh I wasn't even trying to imply that it has O(n^2). The difference is 9ms vs 3ms; just the overhead of calling a function will do that for you.
Admin
The page lets you update the code, so I fixed it and submitted for review. Timing results came out a lot closer.
Admin
"but just demonstrates another way in which PHP can harm you" Does PHP cause cancer in California?
Admin
"why would you write a for loop, with a counting variable, when you could do this instead?"
So... a bit like Python then.
(Yes, that's exactly how Python does all its for-loops)
Admin
Beat me to it.
Admin
It's good to see this on TDWTF, as an ex-colleague used to do exactly this, but using range() rather than array_fill(). Neither he, nor the other 2 PHP developers we had at the time, could understand what was wrong with that way of thinking. He was the one whose SQL queries were always in a loop, reading one row at a time...
Admin
False. Python does not fills anything. Even if you mean range() by 'for loop'.
Admin
The PHP foreach ref thing just seems like a good reason not to use
&$val
in a PHP foreach loop.Admin
Maybe someone challenged him to write a program without using a single 'traditional' for loop. Someone once dared me to write one without using the letter F. Very diicult!