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?
Share
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.