Should i use echo to construct HTML?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Lureren
Forum Newbie
Posts: 8
Joined: Fri Aug 07, 2009 8:07 am

Should i use echo to construct HTML?

Post by Lureren »

Hi

I have a simple question about using echo/print. Should i use echo/print to construct HTML-code?

E.g.:

Code: Select all

$name = $_GET[name];
 
echo "<p>" . $name . "</p>";

Code: Select all

$name = $_GET[name];
 
<p><?php echo $name; ?></p>
Which is best, and why?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Should i use echo to construct HTML?

Post by AbraCadaver »

It is really personal preference, but the trend is and what seems to be more maintainable is to separate display and logic. So what is commonly done is to do PHP logic related stuff and then include a file that is mostly HTML but has some echos in it as in your second example. This also makes it easier for developers and designers to work together as the files for display are not littered with loops, db queries etc...
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: Should i use echo to construct HTML?

Post by Griven »

Like AbraCadaver said, it's a personal preference. I use both methods in my projects whenever they seem appropriate.

For example, when I need to use a short bit of HTML in my code, I'll just echo it like so:

Code: Select all

//Lots of PHP
echo '<p>A short bit of HTML</p>';
//Lots of PHP
However, if the block of HTML is large, I'll escape PHP and just use pure HTML.

Code: Select all

//Some PHP
?>
<p>Just pretend this is a crapload of HTML</p>
<?php
//Some more PHP
 
User avatar
timWebUK
Forum Contributor
Posts: 239
Joined: Thu Oct 29, 2009 6:48 am
Location: UK

Re: Should i use echo to construct HTML?

Post by timWebUK »

Someone on this forum showed me a heredoc. That was quite useful for outputting large amounts of HTML (that contained a lot of PHP variables)

Code: Select all

$hello = <<<HELLO
    <b>$myVar</b>
    <b>$myVar</b>
    <b>$myVar</b>
    <b>$myVar</b>
    <b>$myVar</b>
    <b>$myVar</b>
HELLO;
You then would echo $hello where you needed it. It can also be used to keep code quite tidy I find. Sometimes you have a lot of HTML and a lot of PHP variables and it just is far too long winded to go around opening and closing PHP I think.

Manual: heredoc
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Should i use echo to construct HTML?

Post by pickle »

I try to follow your second example as much as possible. I don't like having my output in multiple places & it keeps my PHP code & HTML code readable.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply