Is there any advantage (in terms of speed) of using the first method before the second?
Is this
foreach ($variable as $key => $value) {
$items .= '<li>'.$value.'</li>';
}
echo '<ul>'.$items.'</ul>'
faster than this?
<ul>
<?php foreach ($variable as $key => $value): ?>
<li><?php echo $value; ?></li>;
<?php endforeach ?>
</ul>
Have no doubt that second is more faster than first one cuz first have one more process( inputting to $items
) while second is directly printing the value. Here you can check more about php code optimization.
Pros:
Cons:
There are lot more and choice differ from person to person what to use and what to not. But I personally hate when php codes and HTML codes are mixed together.
<ul>
<?php foreach ($variable as $key => $value): ?>
<li><?php echo $value; ?></li>;
<?php endforeach ?>
</ul>
This is the faster option as we are not using a variable in this thats why server response time will be increase and it will be faster... :)
No your first code is less faster than the second code because when you run first code it will reserve memory $item. if you will run loop 10 times it will increase the memory of variable. that effects the speed of your code. in second code there is no extra memory utilization so it will work more fast than the first code because memory consumption is less.