user796443
user796443

Reputation:

is there any difference between these two ways of outputing html content with php performance wise?

<?php
            foreach($pricelistline as $value) {
                $e = explode(",",$value);
                if ($e[0]) {
                    echo "<li>\n<img width=\"24\" height=\"24\" src=\"features_icons/" . $e[0] . ".png\" alt=\"\" class=\"\" />\n<span>" . str_replace("-", " ", ucfirst($e[0])) . "</span>\n</li>\n";
                }
            }
?>

And this_

<?php
            foreach($pricelistline as $value) {
                $e = explode(",",$value);
                if ($e[0]) {
?>
                    <li>
                        <img width="24" height="24" src="features_icons<?php echo $e[0]; ?>.png" alt="" class="" />
                        <span><?php echo str_replace("-", " ", ucfirst($e[0])); ?></span>
                    </li>
<?php
                }
            }
?>

In general is there any difference performance wise? Is there any difference in speed and load?

Does exiting and entering php multiple times degrade performance? <?php >

Which one is considered a better practice?

Is there any difference between 1 instance of echo vs 2 instances?

Upvotes: 2

Views: 86

Answers (2)

Martijn
Martijn

Reputation: 3754

The PHP compiler will probably parse both to pretty much the same with not a significant performance difference. In general the way you output in PHP isn't a major performance concern.

You'd probably save a lot more performance by putting $e[0] in it's own variable, optimizing some other parts of the logic or using ob_start('gz_handler') to compress the output for faster transmission.

That being said, why not use microtime() and iterate that particular code a few thousand times? When it comes to performance, measuring is always better than somebody's opinion (including mine). Even better, use XDebug or a real profiler to find out where the processing time is being spend.

Upvotes: 0

Core Xii
Core Xii

Reputation: 6441

Entering and exiting PHP, as opposed to echoing strings, has the advantage that a text editor can syntax highlight the HTML. If it's inside a PHP string, it cannot.

Performance-wise, I believe the difference to be completely insignificant and therefore irrelevant. This is a case of premature micro-optimization. Whichever method you choose, it is sure not to be the bottleneck in your application. If your application isn't performing adequately, then do profiling to identify what's actually taking too long.

Upvotes: 1

Related Questions