In my php files, I wonder if the opening and closing tags have a performance impact.
ie.
<?php echo 'Hello world'; ?>
<?php echo 'Hello world'; ?>
<?php echo 'Hello world'; ?>
<?php echo 'Hello world'; ?>
<?php echo 'Hello world'; ?>
vs.
<?php
echo 'Hello world';
echo 'Hello world';
echo 'Hello world';
echo 'Hello world';
echo 'Hello world';
?>
If so, what is the impact?
Does it have a performance impact? Yes. Simply because there is more text for the parser to parse. It must have an impact. And yes, it will have a measurable impact.
Does it have a meaningful impact? No. Not at all. You would be hard pressed to see any difference even if you had millions of them in your app. Yes, there will be more clock cycles used, but trivially...
The bigger thing is to note that the two pieces of code are not identical in general. PHP will strip a single new line after a closing ?>
, but any other characters after ?>
will render in the output. So trailing spaces or multiple newlines after ?>
will be rendered directly.
So my suggestion is ignore the performance, write the correct and more readable code (the more semantically correct code). And ignore small performance differences...
I would say consolidate inside PHP wrappers as much as possible without sacrificing readability/functionality. Unless you specifically have a reason to break (or use) PHP (such as to enter a decent amount of HTML), why do it?
The performance impact is minimal, and it's all server side anyway. You can test your page load times if you're really concerned, but I would wager the time it takes to test it is exponential to the actual load it would add.
Take for example:
<?php
$foo = 'writing code';
echo "<h1 class=\"hstyle4\">Hello World</h1>";
echo "<p>I am {$foo}, specifically HTML, inside of PHP using echo.";
echo 'but I could just as easily have broken it into html and used only what I needed.</p>';
?>
Versus
<?php $foo = 'writing code'; ?>
<h1 class="hstyle4">Hello World</h1>
<p>I am <?php echo $foo; ?>, specifically HTML, outside of PHP. It's probably a lot more readable this way, and doesn't impact the server nearly as much by parsing unnecessary code that could have easily been handled another way.</p>