tags:

views:

364

answers:

8

I'm a fairly experienced PHP coder, and am just wondering what the best way of echoing large chunks of HTML code is (best practise).

Is it better to do this:

<?php
echo "<head>
<title>title</title>
<style></style>
</head>";
?>

or this:

<?php
define("rn","\r\n");
echo "<head>".rn
."<title>title</title>".rn
."<style></style".rn
."</head>".rn;
?>

I tend to use the second as it doesn't mess up indenting in the php source. Is this the way most people do it?

+4  A: 

Dont forget you also have access to HEREDOC (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

echo <<< HERE
<head>
   <title>title</title>
   <style></style>
</head>
HERE;

Also, I would look into something like Smarty templates - and more importantly the MVC design pattern which enforces separating markup from business logic.

Mr-sk
+8  A: 

IMO, The best way is typically to store the HTML separately in a template file. This is a file that typically contains HTML with some fields that need to get filled in. You can then use some templating framework to safely fill in the fields in the html document as needed.

Smarty is one popular framework, here's an example how that works (taken from Smarty's crash course).

Template File

<html>
<head>
<title>User Info</title>
</head>
<body>

User Information:<p>

Name: {$name}<br>
Address: {$address}<br>

</body>
</html>

Php code that plugs name & address into template file:

include('Smarty.class.php');

// create object
$smarty = new Smarty;

// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');

// display it
$smarty->display('index.tpl');

Aside from Smarty there's dozens of reasonable choices for templating frameworks to fit your tastes. Some are simple, many have some rather sophisticated features.

Doug T.
+1 for Smarty!!
jldupont
forget about smarty: http://nosmarty.net/
erenon
I don't actually use smarty, it was the first thing that came up when googling "php templating"
Doug T.
A: 

The first form is more efficient, as it does not perform string concatenation (.).

Traveling Tech Guy
Isn't this micro-micro-optimization? With very little actual effect?
Pekka
Traveling Tech Guy
+5  A: 

You could also place your HTML outside of the PHP code block:

<?php
    // PHP code
?>
<head>
<title>title</title>
<style></style>
</head>
<?php
    // further PHP code
?>
Gumbo
+1 for this solution. And if you wanna echo variables, use this shortcut <div>Some HTML...<?=$variable?>...and more HTML.</div>
Daan
@Daan "short open tags" are not recommenedable. See http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use
Gordon
Daan
@Daan: *Short open tags* should be avoided to provide the best portability for an application.
Gumbo
A: 

user heredoc syntax

echo <<<NO_MORE

<head>
<title>title</title>
<style></style>
</head>

NO_MORE;

or just escape from php output html directly ?> ... <?php

Scott Evernden
Why is this downvoted? What's so bad about HEREDOCs?
Marcel Korpel
+2  A: 

I think neither is the best solution. Echoing html is not very readable. I always put the html outside of the php tags. This way, you get the indentation, and it's more readable too.

Something like this:

?>
<head>
    <title>title</title>
    <style></style>
</head>
?>

And if you want to take it further, you can use a template system to really separate the logic from the presentation.

Ikke
A: 

Since there is already enough alternatives given, I'd just comment on your second approach. If you want to do it with echo, it is better to use:

echo "<head>", PHP_EOL,
     "<title>title</title>", PHP_EOL
     "<style></style", PHP_EOL
     "</head>", PHP_EOL;

Using PHP_EOL instead of a custom constant does not litter the global scope with a superfluous constant and also makes the newline OS-independent, as it will use what is defined for the OS PHP is running on.

Using commas instead of concatenation saves you some memory due to PHP not having to concatenate at all, but just flushing whatever you throw at echo.

Gordon
A: 

If you're using quotes to display HTML, use the single quote ' ones because PHP searches for variables into the double quotes ", which is less preferment for showing static strings.

For me, the best method remains the one outside the PHP block, some like others answered.

Also, don't forget the shortcut.

Daan
While I find it generally easier to read single quotes and thus use them whenever possible, the (neglectable) speed difference is dependent on what you are doing with the string, e.g. `'foo '.$bar.' baz'` is not necessarily faster than `"foo $bar baz"`. Also see http://www.phpbench.com/ (at the bottom)
Gordon
Thanks for that link, it's a really great website :D
Daan
+1 thanks ofr the great link!
Marco Demajo