Home > Programming & scripting > One of the main reasons that so many PHP-coders suck at programming?

One of the main reasons that so many PHP-coders suck at programming?

February 2nd, 2008

I was about to just write a very small thing in PHP, and wondered what the nicest way to get a random string i PHP would be (lacking a nice sample method like in python) and googled “random string php”.

I found this.

Just… what is that? It just might be the ugliest piece of code I’ve ever seen. Not only does it have a nice little switch statement for each letter in the alphabet, for some reason the coder also seeds the random seed for each iteration in the loop.

This might be the worst I’ve seen, but I’ve seen a lot of PHP example code that is pretty close, and I think that this might be one of the main reasons that a lot of PHP-developers produce unreadable code (have a look at tbsource among other large projects for some horrifying code).

It’s nice that people like to share their code to teach others, but you are more likely to hurt development if you don’t have a clue what you are doing.

I’m not saying that all PHP-developers are horrible programmers (because they aren’t), and I use quite a few PHP-applications myself (such as wordpress) but the ratio of bad code compared to other languages seems to be way above average.

buffi Programming & scripting

  1. robert
    February 2nd, 2008 at 20:29 | #1

    do not knows man you don’t did answered the questions in title of articles you ask and understand your frustration I do but I to say have that your engrish awful is. also me understand not why use you “`” instead of “‘” uhm… looks more elite thinks I?
    And now, regarding the subject, well if you really want to see some ugly source code try to take a look at MySQL’s (written in C & C++) source code. Think again, take another look at MySQL’s source code, think again, re-write this article. Oh and don’t forget about the grammar.

  2. February 2nd, 2008 at 21:12 | #2

    Wat?

  3. Jani
    February 5th, 2008 at 21:22 | #3

    It is true that a lot of the PHP-examples out there are more or less… crap. I think this spreads by people learning from bad examples and then posting the same sort of bad code themselves. Maybe this is because PHP is a forgiving language (at least I think so) and therefore beginners can easily get started with it without really understanding what they are doing. I never really understood what I was doing when as a beginner at programming wrote some PHP-code. It all came to me first when I began with languages like C++ and C#. Thanks to those languages my PHP-code today is more refined and now I actually understand what I am doing. But maybe it only applies to me.

  4. Jani
    February 5th, 2008 at 21:25 | #4

    Checked out the website you linked to and also found this:
    http://www.i-fubar.com/flip-a-coin-script.php

    Interesting way of checking for odd/even numbers I must say.

  5. kalle
    February 6th, 2008 at 12:51 | #5

    So what code did you end with to get a random string in PHP? Wouldn’t it be nice with some good examples that Google can find in the future? :-)

    Also, you could explain why the bad code you found are bad, it would surely help development.

    Jani: It applies to me aswell.

  6. February 9th, 2008 at 13:40 | #6

    I didn’t need a “true” random string as it was only meant to be used as a salt so I just went with the example from the uniqid-page at http://se2.php.net/manual/en/function.uniqid.php, that is.

    $token = md5(uniqid(rand(), true));

    If I wanted randomness that includes the entire alphabet I would probably just write a sample-function which takes an array, randomizes an array position and returns the value at that position. I’m unsure how this would work for non-incrementing arrays not based at zero (in other words associative arrays), but there should be some quite easy work around for that as well.

    “Also, you could explain why the bad code you found are bad, it would surely help development.”

    I did in the second paragraph. An INSANE switch statement, and setting the random seed for each loop iteration is basically the highlights.

  1. May 14th, 2008 at 19:16 | #1