So curiosity got to me. I always try to avoid creating variables that will only be used once, instead I try to use it directly (unless it's a lot of text, for example MySQL queries).
What I usually do is something like this:
$getSomethingSql = 'LONG SQL';
$getSomething = $dbConnection->prepare($getSomethingSql); // or query if no parameters are needed
// Some binds etc if needed
$getSomething->execute(); // if prepared statements are used
$something = $getSomething->fetchAll(PDO::FETCH_ASSOC);
foreach ($something as $s) {}
Now, the $getSomethingSql
is only used once but is stored as a variable since it can be a long string and thus this looks better. However, I was curious if there is any advantage of using above versus the code below:
$getSomethingSql = 'LONG SQL';
$getSomething = $dbConnection->prepare($getSomethingSql); // or query if no parameters are needed
// Some binds etc if needed
$getSomething->execute(); // if prepared statements are used
foreach ($getSomething->fetchAll(PDO::FETCH_ASSOC) as $s) {}
Does these 2 codes do the same? And is there a (huge) performance difference, or is one of them cleaner?