views:

138

answers:

5

Heya guys, now ive never done this method before and i just tried it to see if it would work and it works like a dream.

Usually people tend to do this way.

$tags = array();
while($row = $statement->FetchObject())
{
     $tags[] = $row;
}

but would it be faster or just less code if i done it this way.

$tags = array();
while($tags[] = $statement->FetchObject()){}

Just Curious that's all


Update:

I do understand that Cleaner code is much better then Less code, but as I never used this method before it was mere curiosity for pros and cons.

+1  A: 

I find the former much more readable and easy to grasp, and I'm sure any performance difference between the two is negligeable.

I'm all for 1).

Pekka
A: 

Sure, as long as FetchObject() changes its return value appropriately each time it's called ;)

Luca Matteis
+13  A: 

The general issue is that to exit the while loop, a "false" result needs to be returned. In your second example, that means there will be a "false" value (which is likely not what you want) at the end of your array.

This is not an issue for the traditional approach because the "false" value is given to $row and never applied to the array.

As for performance, or readability, they're non-issues since the code doesn't do what you want it to do.

salathe
Very good point, i just tested it and your 100% correct
RobertPitt
+2  A: 

And if you're database class allows it, simply use the predefined method for this purpose. PDO for example has fetchAll: $statement->fetchAll(PDO::FETCH_OBJ).

nikic
+500 for this one. Was going to leave a comment with same point
Col. Shrapnel
Not +500 lol, i know about FetchAll but the reason i haven't used it here is because there's more code within the loop that i ahve removed, such as `$row->tags = $this->getTagsForPost($row->feed_id);` for Many-to-Many queries.
RobertPitt
@Robert looks like you have no do it using SQL. Anyway there is no reason do do anything in the getting data loop. If you want to loop over this data - get an array and iterate it.
Col. Shrapnel
@Robert oh no you little liar! :) There is no use for the first method then.
Col. Shrapnel
I think the question is more about the technique, not what the code is doing. There are other times (not just bad DB interaction) this 'style' could be used.
Tim Lytle
@Col, the while loop that made me think originally was within the getTagsForPost(), I just posted some code here as an example. Dont be rude!
RobertPitt
@Robert my apologies if you took this rude, I didn't mean it. But I still see no point in the question. If `there's more code within the loop`, as you said, the first one is not applicable and there is no sense in the question at all. Where I am wrong?
Col. Shrapnel
your getting it wrong, when I said that theres code already in the loop was refering to the section of code in my project, when i tested the 2nd version it was in another section of my applications, where there was no other code in the block, as I said originally this was mere for pro's and cons
RobertPitt
+1  A: 

You could even skip the brackets:

$tags = array();
while ($tags[] = $stmt->fetchObject());

That code certainly is a bit shorter that it's more verbose form:

$tags = array();
while ($tag = $stmt->fetchObject()) {
    $tags[] = $tag;
}

However which one is easier to read? You could say both are quite obvious and actually I would agree with you. But which one is easier in general maintenance? To add a new statement like $tag->doSth(); in shorter form you have to completely rewrite it. In the last one you just add that statement.

Crozin