If you don’t know PHP, you can safely ignore this.
I had been using the following code to get rows out of a database (it’s with Oracle, but I’m sure mysql_fetch_assoc() would work just as well for these):
while ($row = oci_fetch_array($result)) {
$whatever[] = $row[];
}
But then it hit me: if I’m already fetching the next row of results in the while() statement, and only copying the results into another array, couldn’t I compact the code more? I rushed to that old sandbox of mine, test.php, and tried:
while ($whatever[] = oci_fetch_array($result));
And by gum, it worked.
I’m assuming there will be one of two reactions from people who read this far:
- Well, yeah.
- That’s horribly bad practice because…
But considering I’m entirely self-taught, I’m fairly proud of myself. And if I shouldn’t be, please do let me know.
Edit: And there it is! Since the loop creates the new item in the array and then fills it with the value of oci_fetch_array, there’s always an empty value at the end that messes with foreach(). It was a fun idea while it lasted, though.
Continue Reading