• (nodebb)

    Not a word about failing to just format the random number into the filename string? I am disappoint.

  • (nodebb) in reply to Steve_The_Cynic

    Apperantly, it dwarfs the WTF that is PHP(-documentation.) But yes, at first glance that was where I thought the article was heading. <groan>

  • (nodebb) in reply to Steve_The_Cynic

    And now you've ruined the joke.

  • (nodebb) in reply to Steve_The_Cynic

    Nah, that's actually accidentally the same instead of really code repetition. Nothing stops them from adding another image in a different path. The switch solution would still work, but the path manipulation would not...

    This one is not terrible code... It is clumsy... cumbersome... but not terrible...

    I guess I would have written it similarly. Would have used an array instead of a switch statement. Would have used the length of the array as the rand max... To remove the magic number that will probably break on extending or reducing it. To make adding and removing pictures easier. And make it easier to move the array into a config file or the database later... Or would have put it there right away if it was allowed. But all in all, that's passable code.

  • (nodebb)

    Hey, it could have been worse. At least, there are no loops to count from 1 to the random number.

  • (nodebb) in reply to Mr. TA

    At least, there are no loops to count from 1 to the random number.

    You would need such a loop for the array version suggested by TrayKnots, because you can't guarantee that PHP gets the count correct or correctly stores a URL at each element.

    (I've just come fresh from reading yesterday's WTF.)

  • LZ79LRU (unregistered)

    The real WTF here is that this code most likely does not do what the guy making the requirement wanted it to.

    Remember, what we developers mean when we say random is not the same as what the average person does. For us random means unpredictable. As in you newer know what the next image is going to be. And this code matches that.

    But designers typically want something that is closer to a random shuffle. As in, they don't want the same image to show up back to back, they want to cycle through all the images before repeating etc. Stuff like that. And this code most definitively won't work like that.

    So if my experience is any indicator it's very likely that this is bad code that the original customer isn't happy with either.

  • Andrew (unregistered)

    At least this logic is in one header file. It would be worse if the logic was duplicated on every page.

    I once worked for a company, where their main app had menus and submenus hardcoded into every page. Each one had at least one item that was misaligned to the others, and every page had a different one, even ones that you'd think would be copied from another.

  • Anonymous') OR 1=1; DROP TABLE wtf; -- (unregistered)

    PHP is just using C's rand() function, at least up through PHP 7.1 when it switched to the Mersenne Twister. All of the problems and caveats listed there are the same as in C. For once, PHP isn't TRWTF here.

  • (nodebb) in reply to Andrew

    At least this logic is in one header file. It would be worse if the logic was duplicated on every page.

    That is exactly the first thing I thought too. :-D

  • serrif (unregistered)

    There are two wtfs not mentioned in the article: First is that PHP switch statements use "loose" equality, so even though the case statements are matching on string numbers but $pic is an actual integer, loose equality makes it still work. Second is that they seem to be completely unaware of PHP string interpolation

  • zboot (unregistered) in reply to serrif
    Comment held for moderation.
  • (nodebb)

    thanks

    Addendum 2023-08-21 23:07: During my recent online escapade, I uncovered a hidden gem - a website housing an impressive collection of stock photos. These high-quality images, covering a myriad of themes and moods, have become an invaluable resource geometric pattern vector in my creative toolbox. From enhancing visual aesthetics in web design to creating eye-catching marketing materials, these stock photos are my newfound source of inspiration and impact.

  • chanduh (unregistered)
    Comment held for moderation.
  • N.N. (unregistered) in reply to Eric Cartman

    Link spam bot. @editors

    Speaking of which: Why is there no report function in this comment section?

Leave a comment on “Randomly Switching Images”

Log In or post as a guest

Replying to comment #:

« Return to Article