Many Ways to Integrate HTML into PHP
It’s a good practice to separate back end logic (PHP) from front end markup (HTML). That isn’t always possible or the best choice, especially if you are working on a small web application. Here’s some of the ways to cleanly integrate HTML into PHP.

Times to use this
As I said above, separating the back end logic from the front end markup is a great practice. I don’t always do this, though. Unless you’re using a template engine, such as Smarty, it isn’t always practical to separate the back end from the front end. You still should if the project is large enough, but if it isn’t it’s not worth it in my mind.
When I described my method of organization for most projects, I had a directly called ‘elements’ that contained all of the front end elements. The scripts in that folder still contain logic, though. I don’t use a template engine, so it’s impossible to have completely segregate the logic from the markup.
Integrating HTML
Instead, here’s a few ways to integrate HTML into your PHP logic while still keeping things clean.
Double quotes and braces
This is probably the method that I use most often. Here’s an example:
echo "<div>{$variableOne}</div>";
echo "<div class=\"class_one\">{$variableTwo}</div>";
echo "<div class=\"class_two\">{$variableThree} to {$variableFour}</div>";
I find that this is really clean and easy to scan. It’s easy to see the variables in the code since they all begin with {$ and end with }. I also use code highlighting, so that helps.
Single quotes and concatenation
This is another popular method. The speeds of this method and the previous have been debated, but I don’t think that it really matters. The difference is in nano seconds. If you really want to optimize things, there are a lot more changes that you could make that would have more of an effect than switching between single quotes and double quotes.
echo '<div>' . $variableOne . '</div>'; echo '<div class="class_one">' . $variableTwo . '</div>'; echo '<div class="class_two"> ' . $variableThree . ' to ' . $variableFour . '</div>';
An advantage of this method is that you don’t have to escape double quotes that may appear in HTML. I find this method harder to read than the previous, especially if there are short strings between variables. It’s hard to quickly tell the difference between $variableThree . ' to ' . $variableFour and $variableThree . 'to' . $variableFour, especially if you include spaces between the periods as I do.
Jump out of PHP
This method is most popular for large blocks of code. It can great really repetitive to just keep outputting HTML from PHP, so why not just write the HTML and then add in the PHP where necessary?
<div> <?php echo $variableOne; ?> </div> <div class="class_one"> <?php echo $variableTwo; ?> </div> <div class="class_two"> <?php echo $variableThree; ?> to <?php echo $variableFour; ?> </div>
An advantage of this method is that it’s much easier to retain the indent structure of HTML. A disadvantage is that it can get repetitive to keep using to output variables. It’s possible to use the shorthand =$variableOne?>, but that may not work on all servers. It also doesn’t work as well if you have a lot of logic and conditional statements instead of just variables.
A combination of them all
The best choice is probably to just use all of these methods as needed. You should decide on whether you will use single quotes or double quotes and then be consistent with your decision. Then, jump out of PHP where needed and use single quotes or double quotes where needed. Here’s an example of a combination:
<div>
<?php echo $variableOne; ?>
</div>
<div class="class_one">
<?php echo $variableTwo; ?>
</div>
<div class="class_two">
<?php echo "{$variableThree} to {$variableFour}"; ?>
</div>
Since it feels really redundant to jump into PHP, echo a variable, jump out of PHP, output a string, jump back into PHP, echo another variable, jump back out of PHP, I combined that part to just use double quotes. This method probably best represents the way that I code. I may have used double quotes for this, since it would only be three lines, but for the example I expanded it and then added in the PHP.
What about you?
What methods do you use to integrate HTML into PHP? Do you use any methods other than the ones listed here? Please leave a comment, I look forward to hearing your thoughts!
(thumbnail by f-l-e-x, banner by Powerhouse Museum)
Tags: Clean Code, Code Organization, Integrate HTML Into PHP, Web Development










Comments:
What methods do you use to integrate HTML into PHP?
I use a model-view-controller framework like everyone else on the planet who isn’t a complete knob.
It was so usefull for me, thanks for info!
One other way to add large HTML code blocks and not incur the cost of jumping in and out of PHP is to use the HEREDOC syntax, like this.
$fn = ‘testfunction’;
function testfunction() { return ‘ok’; }
$string = <<< heredoc
plain text and now a function: {$fn()}
heredoc;
I prefer the third but it’s better write php in html.
In this exemple, it’s better:
with and not display in brut text ;-)
cordialy
sorry last comment
Comment system takes out tags.
let me try again here: <?= $variable ?>
Yes I use <?= $var ?> alot but I never see other people doing it. Is it wrong?
<title><?= $pageTitle ?></title>
Seem pretty logical to me.
However to make things easier at work, we put all of our websites into a content system. Everything that is programming has to produce standard HTML UL/LI and DIVs and all things need to have a class name or similar. Then programming is wrapped into a tag, ex: {navigation}.
Designer then has to style with CSS the output. My company used to work design first then developer. Everytime it went wrong. Develop first designer later works better and faster.
@Greg – Well, I wouldn’t say that everyone who doesn’t use MVC is a complete knob. Thanks for the comment though!
@Trey – I decided to leave out heredoc syntax. Personally, I don’t like it at all. I probably should include it for completeness, though. Thanks for the comment!
@sebounet – WordPress took out all of your code, so I’m not sure what you’re saying.
@Rob – Yeah, sorry that WordPress spliced your comments so bad. Using <?= only works if short_open_tag is set to 1: link (it’s the first item on the page). So it’s safer to just use <?php echo unless you are sure that it will be enabled. Thanks for the comment!
I use a combination of the “Jump out of PHP” method and heredoc/nowdoc.
I’ve experienced the “<?=" issue on a server with short_open_tag disabled (I believe it was on a recent version of XAMPP running on a Windows box) and ever since have been using echo instead to simplify deployment.
I use a template parser. But in the unlikely case that i should need to use pure PHP in a template i would use
Greg, MVC? Really? So you must be not the “complete knob” for that matter ;) Well, as far as I know MVC does divide your logics and page layout, but how “not knob” is it to say that MVC cuts out php tags in your view?
Who ever said that? Nobody knows exactly what MVC is, that’s what the dev guys at Google said last year @ Google Developer Day, thus introducing the MVP (Model-View-Presenter) model ;)
But still, MVC, MVP, whatever. If I wanna put a damn title between my title tags in the head section of my html layout, how the hell am I supposed to do that without the php tags? Lemme guess – string replacement with a search for [my-title-goes-here] in the model? ;) Omg, are we back to Smarty?
Austin, Good post. ;)
Cheers,
~ K.
The <?= I thought was called the evaluation tag? Oddly I have been PHP-ing since 2000 and never came across a server that didn’t have the evaluation tag turned on. I have seen many thank god that have the ASP % tags off.
Either way. In regards to the MVC fanboy comment, is it really necessary to use a full blown MVC framework if all you are doing is taking data from a form and sticking it into database? Isn’t like rule #1 of being a good programmer is to re use code? You’re saying that you have to use and MVC Framework to do basic CRUD cause you don’t have your own code to re-use.
@Greg – If by “knobs” you mean those of us with the real-world experience to not be mindless followers, then you would not realize that MVC is child’s toy, unlike the more nuanced enterprise architectures. Perhaps, once you’ve coded for a few years, you’ll realize that there are occasions that call for over-engineered frameworks, and situations where you have 30 minutes to create a solution before total annihilation.
Agree with everyone who ripped Greg a new one. Real-world and ideal-world are different places, and when he’s had some real-world experience with large sites and large clients, he’ll realise this.
Good article Austin – helpful for the new folk, for sure.
I wrote myself a HTML engine.
I put every HTML tag into a PHP class and gave them the ability to render as html.
Currently i am writing a site kit to simplify the site coding.
Sepparation of back end and front end is defined by your discipline of sticking to MVC, not by sepparating HTML and PHP.
@Nitin
I’m glad to hear that I’m not wrong in suggesting not to use ‘<?=’! Thanks for your comment!
@Intrepidity
The code in your comment was removed by WordPress, so I’m not sure what you’re saying. Thanks for the comment though!
@Konstantin
Thanks for the comment. I agree with you! MVC still requires PHP to be inserted into HTML. And it’s wrong to assume that anyone who doesn’t use it is a ‘complete knob’. There are many times when MVC isn’t the right solution.
@Rob
Nitin (above) said that he has used a server that has it disabled. I’ve never worked with a server that had it disabled but I still think that it’s safer to keep it out, especially if you are distributing code. Thanks for your comment!
@Iain
Thanks for your comment! I definitely agree with you on the MVC issue.
@Isabelle
That’s definitely a method that works. I tend to stay away from doing things like that for smaller projects since it seems like overkill. I’ve done it with larger projects, though. Thanks for your comment!
Trackbacks: