The PHP WTF

Tweet

This site keeps popping up on my radar recently, inspired by this site.

Although things like this are quite amusing, got mixed feelings about the idea in general. On the one hand anything that helps people improve their coding skills is a good thing. On the other hand, think the approach being taken here is a kind of elitism which will discourage asking of “dumb” questions; that the overall effect is negative. Ignorance can (and should) be forgiven in programming, IMO – no one knows it all.

More specifically this particular PHP WTF raises my doubts further – reading it you might get the idea that mysql_num_rows() is a bad idea period, which isn’t the case. There are situations where it has a value in determining the size of a result set you are about to use but reading this you might come the conclusion that the right solution is always to use two queries like;


$query = 'SELECT COUNT(*) FROM sometable';
$result = mysql_query($query,$connection);
list($count) = mysql_fetch_array($result);

$query = 'SELECT * FROM sometable';
$result = mysql_query($query,$connection);
while ( $row = mysql_fetch_array($result) ) {
// etc.
}

The other, much bigger problem, that was missed in the discussion was the impact of ‘SELECT * FROM sometable’ – a full table scan. If possible, would be nice to narrow down the result set with some WHERE clauses and take advantage of indexes. There’s a good discussion here (Google cache) in MySQL Performance Tuning from The Man.

That said, think this WTF was better argued.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • http://www.nudeshirt.com Findus

    It all depends on how you read it.
    I dont read that as serious, to me it seems more like a humor site for php programmers/scripters.
    I totaly agree with the example you used that its not good to do it in the way they did if people actually take this as a serious coding advice.
    In this particular example it could lead to newbies using one extra query for each time they wanted to count something.
    Atleast they should use an example of a more real life query to illustrate.

    But in the end, i woulc consider the site a fun read, not a resource.

  • http://www.gesf.org gesf

    Personally, i think it can be a fun read when we don’t make the same mistakes as posted/shown. For example, i consider the query’s case, fun to me… don’t need to say why…!
    However, it can be interesting and also didactic in the way they show us (newbies or not) a better and efficient coding way. Or just the MD4’s case.

    Hope they don’t turn it into a complete drollery website.

  • David

    His rewrite in th launch WTF example has also some possible errors: He assumes that $arrReferralName is not an empty array and I would also put some single quotes around $codeData to make a valid SQL :)

  • http://www.bittime.com transio

    I dunno about MySQL, but in some RDBMS’s, the fastest way to get a COUNT() is on an index column:

    SELECT COUNT(index_column) FROM table_name;

  • http://nathanwwong.com someonewhois

    It looks as if it suggests what NOT to do, yet I personally interpret it as what TO do, the way it’s written.. though I know better.

    That md4 post – what on earth is the point in that? Seems rather useless, I agree with Findus, it’s a good joke.

  • http://www.pswebmin.org p4schroeder

    Can someone enlighten my ignorance about WTF? What is that and what does it stand for?

  • http://aplosmedia.com/ Eric.Coleman

    stands for what the f**k….

    ;)

  • http://www.aska.id.au/ Aska

    [quote=p4schroeder]Can someone enlighten my ignorance about WTF? What is that and what does it stand for?[/quote]Correct me if I’m wrong but I think WTF = “What The F%#@”

  • http://www.aska.id.au/ Aska

    stands for what the f**k….

    Oh no. Beaten by 10 minutes! I knew I should have refreshed the page before posting. Anyway to delete a comment?

  • http://blog.casey-sweat.us/ sweatje

    I thought it was “What the fsck” ;)

  • http://webtech.lv kaklz

    I don’t think you should take this site as a manual or tutorial – it’s just a humor site and it’s interesting to look at the “solutions”.

  • http://www.phppatterns.com HarryF

    Guess that makes me Mr Nohumour ;)

    Just think a more worthwhile way to spend time and effort would be something between a FAQ and ActiveState’s PHP Cookbook.

  • http://www.nudeshirt.com Findus

    Like i said, humours.
    But not everyone gets that, and for a newcomer to php he could take this as a “serious” ressource.

  • Alex Papadimoulis (from TheDailyWTF)

    I really do my best to avoid being an Elitist Bastard. I will not post things from newsgroups or the like, only things from real, paid professionals. And when you are a paid professional (not a hobbyist), there is no excuse for “I didn’t know any better.” If you don’t know any better, then get a different job …

  • http://www.redcow.ca/ Ray Oliver

    I think it is a fun read, but it would be nice if the author had better spelling and grammar skills.

  • Anonymous

    But even in that situation would it not be more effecient to

    mysql_query(‘SELECT count(*), * FROM sometable’);

    rather than mysql_num_rows()? That is then one query, instead of 2 or 1 with a function.

  • Captain Proton

    But not everyone gets that, and for a newcomer to php he could take this as a “serious” ressource

    Ah well, things can hardly get any worse than they are now :)

  • http://blog.casey-sweat.us/ sweatje

    I think for the most part everyone is over reacting a bit. The site is obviously targeted at a _slightly_ more sophisticated audiance than the PHP beginner, they have to at least be able to recognize the poor coding as such or the irony is lost. Also, the author seems to be presenting reasonable work arounds for each piece of problem code.

    From the general tone of the articles, I think even a beginner would easily recognize that these were not examples to be modeled.

    My $0.02

  • http://www.lunadesign.org awasson

    Good site and a good chuckle. I don’t expect everyone will get (or like) it but anyone who’s been there and recognizes the code will get a good laugh.

    …or not.

    Maybe not for newbies, but sometimes you do just have to say WTF ;-b