Look at this:
<?php
echo "Hello World";
?>
<br />
<?php
echo "Welcome";
?>
And now look at this:
<?php
echo "Hello World";
echo "<br />";
echo "Welcome";
?>
Which one of the above examples is considered to better (at least from performance point of view)?.
I know the examples are trivial but I want to follow good practice habits from the beginning so when I have more and more lines they would not affect the performance or whatever in a negative way.
8
As comment points out, it should be done by templates. But if only your two choice is possible. Case 1 will be better. This is the same concept as android inflating layout by xml , or programmaticlly making the UI. Your <br/>
is a static content from your html and all your server will be doing is to show it. But if you use php, to execute it, this will be using your servers resources and be computing.
This is how it should be done:
index.php:
<?php
include 'header.html';
...
include 'footer.html';
?>
header.html and footer.html are templates.
8
To directly answer your question: Never echo static text or HTML. Leave that outside of your PHP.
<?php echo "<p>Hello World</p>"; // Alway wrong. ?>
<p>Hello World</p> <!-- Does the same thing, faster, and is more readable. -->
Escaping to PHP is a waste if all you are going to do is pass in static text or static html. All code inside of <?php ?>
is sent to PHP to be interpreted.
This means your website is slower and you are wasting electricity if you are passing large amounts of static text to PHP for no reason. (Sometimes there is a reason.)
Best practice is to have your logic and grunt work in files without any HTML, only PHP.
The way I like to do this is with an MVC framework. I have pure PHP in the models and controllers, and a mixture of HTML and PHP in the views. You don’t need an MVC framework; you could just stick your mixture of HTML in template files, which you then include in your pure PHP files.
Once you have your variables (which don’t include HTML in them) created in pure PHP files, you can pass them off to the file with your HTML. There, and only there, is it fine to mix HTML and PHP. But, and this is an important but, when working with files that have HTML, keep the PHP to a minimum.
Luckily, PHP comes with friendly tools to make PHP easier to read when mixed with html.
Alternative Syntax
The best practice is to use regular PHP syntax when writing pure PHP, but the alternative syntax, when mixing PHP and HTML.
In regular PHP files without any HTML:
if ($something) {
do_something(); // do_something() shouldn't echo or print.
}
This is beautiful for pure PHP, but not ideal when escaping from HTML. In particular, the closing braces are often hard to understand when they show up in big piles of HTML.
It seems, based on your question that you would have written, in a file mixing HTML and PHP, this:
<?php
if ($something) {
echo '<p>Hello World.</p>';
}
?>
For the reasons described above, we want the HTML not to be sent to PHP. We do this like so:
<?php if ($something) { ?>
<p>Hello World.</p>
<?php } ?>
We got our HTML out of our PHP tags, which is an improvement. But we could go further.
Notice how uninformative the closing brace is? We don’t know if it is closing a loop or an if statement. This seems trivial in the above example, but there are often dozens of lines of HTML inside of if/endif inside of a loop/endloop.
The best practice is that in a file mixing PHP and HTML, you use the alternative syntax, which omits opening and closing braces ({ }
):
<?php if ($something): ?>
<p>Hello world.</p>
<?php endif; ?>
Aside from the semantics of the new syntax (:
and endif;
instead of {
and }
), you should notice two things:
- Again, the HTML and text, while controlled by PHP, is outside of the
<?php ?>
tags, so it is not interpreted. For the reasons described above, this is good. - This is much more informative than if this were all php including random closing braces (
<?php } ?>
) closing things that are hard to find where they opened (because they are mixed with a buch of messy looking HTML). Again, imagine a big HTML file with loops and if statements. Specifying what you are closing can clarify a lot.
The same template friendly syntax can be used for loops as well:
<?php foreach ($things as $thing): ?>
<li><?php echo $thing; ?></li>
<?php endforeach; ?>
Short Tags
Another great tool provided by PHP which, again, you only want to use when mixing PHP and HTML together, is short tags. You might have to turn short tags on in php.ini file. Once turned on, short tags make everything even more readable.
Without short tags, you have to write <?php echo $something; ?>
but with short tags you can write <?=$something?>
and it will have the exact same result.
When combining short tags and the alternative PHP syntax, you can really write some elegant HTML files, which include PHP elements. Taking our last loop example (just the part inside the loop), short tags allow us to reduce:
<li><?php echo $thing; ?></li>
to
<li><?=$thing?></li>
which is much more readable.
Could we go further? Yes. Using various templating systems you can do things like:
<li>{thing}</li>
Which is amazingly readable. Templating systems, however, are way outside the scope of the original question (which I left a while ago anyway).
6
You’re learning, forget about performance in PHP pages. Instead, choose the way that is easiest to read.
Looking at your two examples the second one is most definitely easiest to read, though I think that’s because of the spacing you’ve used. You say you’re looking to develop good habits from the start, I think your time can be better spent by making your code read well. This might mean rewriting a section several times until you’re happy with it.
If you’re really keen on the behind-the-scenes, I’ll have a go at explaining what happens with static content in a PHP page. One of the first things is to “parse” the entire page into executable code (like you see between <?php ?>
tags). One part of this is to turn any static text into an echo
.
So this:
<?php echo "Hello World"; ?>
<br />
<?php echo "Welcome"; ?>
is transformed into something like:
echo "Hello World";
echo "<br />";
echo "Welcome";
There’s a very small cost associated with the parsing, but no difference in executing the resulting code. If you’re using a PHP accelerator the parsing is only done once and then cached, so you would see no performance difference after the first page load.
Remember: easy to read!
2
It is generally considered bad form to output HTML code from PHP. There are several reasons for this, but the main one is that it is easier, especially in the long run, to maintain code if the HTML and PHP code is separated.
It is just like HTML, CSS and Javascript; it is just much easier and more practical to keep it separate.
Technically, however, it makes absolutely no difference how you output stuff. To PHP it is just bytes in a row.
1
One of the best features of PHP was it is designed to be embedded in HTML. It is usually used in creating templates. It makes your code easier to read and maintain, because it separates the PHP code from HTML.
But if your only printing a static page like “Hello World!” it’s a bad idea.
To answer your question simply: given your example code, there will be little if any difference in perfomance. PHP gets processed at the server/host level whereas HTML gets processed by the user agent/person looking at your webpage on their browser. Who will be faster? Difficult to determine.
I agree with others saying each has their strengths and weaknesses and can work really well together. PHP is programmable and can do calculations and work with variables. For that reason all my pages end up being PHP and call on snippets of HTML code – templates as was mentioned and inserts variables so that my HTML becomes dynamic.
In your example you could string the HTML together into one PHP statement like so:
<?php echo "Hello World<br>Welcome"; ?>
Hope that helps clarify a little.
there are limitations on using HTML within PHP.
Most of the time you would have your HTML outside. You would create a page just as you would if you were not using PHP, then add the PHP into the areas of the document where you need it. You can have several chunks of PHP within a document.
Working With PHP Inside HTML has some examples :
https://www.thoughtco.com/php-with-html-2693952