• (nodebb)

    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.

  • Chronomium (unregistered)

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

  • (nodebb) in reply to Mr. TA

    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.

  • my name is missing (unregistered)

    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.

  • (nodebb) in reply to my name is missing

    Not even the aliens themselves kek

  • (nodebb)

    Now, if you're thinking to yourself that array_fill means that there's a direct relationship between these two values, so that's completely unnecessary, you win a prize.

    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)

  • (nodebb)


  • ooOOooGa (unregistered)

    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?

  • (nodebb) in reply to Chronomium

    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.

  • (nodebb) in reply to Barry Margolin 0

    I was half-expecting an off-by-one error.

  • (nodebb)

    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


  • (nodebb) in reply to TheCPUWizard

    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.

  • Sole Purpose Of Visit (unregistered) in reply to Mr. TA

    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.

  • Meh (unregistered)

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

    $array = array("It's", "so", "simple!!");
    foreach($array as &$value){
      echo $value."</br>";
      if ($value=="It's") {
        $array2 = array("Or","is","it??");
        $array = array_merge(array_slice($array,1),$array2);
  • Fizzlecist (unregistered) in reply to TheCPUWizard

    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?

  • (nodebb) in reply to Sole Purpose Of Visit

    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.

  • Raul (unregistered)
    Yes, they populate an array with the range of their loop

    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.

  • 516052 (unregistered) in reply to my name is missing

    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.

  • (nodebb) in reply to Mr. TA

    Instead of using a constant, or a variable, they call the count function on each iteration.

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

  • (nodebb) in reply to Steve_The_Cynic

    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.

  • (nodebb) in reply to Mr. TA

    The page lets you update the code, so I fixed it and submitted for review. Timing results came out a lot closer.

  • (nodebb)

    "but just demonstrates another way in which PHP can harm you" Does PHP cause cancer in California?

  • Jinks (unregistered)

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

  • c (unregistered) in reply to Bananafish

    Beat me to it.

  • CBG (unregistered) in reply to Barry Margolin 0

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

  • Pavilon (unregistered) in reply to Jinks

    False. Python does not fills anything. Even if you mean range() by 'for loop'.

  • (nodebb)

    The PHP foreach ref thing just seems like a good reason not to use &$val in a PHP foreach loop.

  • Programmer Bot 1-32612 (unregistered)
    Comment held for moderation.
  • Chandigarh Escorts (unregistered)
    Comment held for moderation.

Leave a comment on “Another Iteration”

Log In or post as a guest

Replying to comment #:

« Return to Article