There is a guy at work that pretty much writes all of his html markup by using echo statements. It looks really clean and I like it a lot, but I remember hearing it is a bad practice considering it becomes a little much when you have to write large quantities of markup. What is the best practice?

+1  A: 

I always read/heard that it's more efficient to throw out markup from PHP than to switch into and out of the preprocessor.

After all, that's what HEREDOCs are for. A TON of echo statements in a row is probably not as good a plan as one on a larger string.

A micro-optimization at best. You're going to be talking in the millisecond range of improvement...
The speed gain isn't worth it unless you're serving thousands of pages a second, but the developer time saved in not having to remember to escape quotes and whatnot *IS* significant, and makes the "wall 'o text" much easier to read though. HEREDOCs are so much handier in almost every case, it's almost criminal to NOT use them.
Marc B

I have all my html code inside different echo's and I find no problem whatsoever with it.

+3  A: 

This is not necessaryly bad practice. As long is your code is readable, do it this way.

If your project grows larger I might however suggest using a template engine (for example smarty.net).

+2  A: 

Unless you have hundreds of lines of markup, I think that this is a micro-optimization. That is to say that a HEREDOC, echo, print, or between PHP tags doesn't really matter.

In the end, it boils down to what's easiest for you or your team to maintain.

Jason McCreary

It sounds like a bad practice if you consider that someday you might want to change technologies (like from php to asp.net or ruby on rails). Seems like Seperation of Concerns is thrown out the window.

Byron Sommardahl
+5  A: 

Alright given the answers, I felt I should put in my 2 cents.

First up, it depends on the project. Me, personally, I use PHP as my template engine, and not a 3rd party. So I store my view logic in a "template" which is just php markup, which does use a few echos, but the echo statements just echo relevant data and not html code. Breaking in and out of PHP is not bad practice, if done correctly.

Having said that, it is much easier to manipulate HTML in a "template" file where the HTML is on it's own without having to worry about quotes (I know heredoc solves this problem, but it is kind of hard to do a foreach loop inside of a heredoc to display data). So my suggestion is just make a template folder / file for your view logic and use the short open tags, if you want to enable them / risk a client not having them enabled or use the <?php echo $data; ?> to echo out your data in that file and include it when needed.

Sorry if this sounds confusing. But that is my preferred method, as having 10 echos that just display data vs 100 echos that display html and data looks nicer and is easier to maneuver / debug in my opinion.

Brad F Jacobs
There were a lot of helpful answers here, but I personally agree with this one the most. I tend to keep templates for markup only(as consistently as I can) and controllers/models for PHP. I just thought that the guy at work had very clean code and I was curious. This may have been because he was mainly doing the javascript section of our project, and only needed to code small segments of markup.

I think the best practice is to simplify your code. If you have a large number of the Echo statements that can possibly be reused pull them out into a separate PHP file and use php require statement to include that common code so the main file you are looking at is simpler. Even if the can't be reused, but that they are static and should not change, pull them out. No need to clutter you page with information that is just noise.