16

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?

Alex
  • 163
  • 1
  • 1
  • 4

9 Answers9

21

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.
  • 64,223
  • 27
  • 138
  • 202
16

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
  • 643,351
  • 109
  • 780
  • 844
  • 1
    +1 for this solution. And if you wanna echo variables, use this shortcut
    Some HTML...=$variable?>...and more HTML.
    – Daan Feb 27 '10 at 15:46
  • 1
    @Daan "short open tags" are not recommenedable. See http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – Gordon Feb 27 '10 at 16:06
  • 1
    That's only a matter of hosting compatibility, and you can resolve it with a bulk find & replace. Not a good enough reason not to use them. – Daan Feb 27 '10 at 16:58
  • 1
    @Daan: *Short open tags* should be avoided to provide the best portability for an application. – Gumbo Feb 27 '10 at 17:08
10

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
  • 13,174
  • 11
  • 66
  • 101
4

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
  • 39,136
  • 15
  • 78
  • 84
4

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>
<?php

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

Ikke
  • 99,403
  • 23
  • 97
  • 120
3

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
  • 312,688
  • 75
  • 539
  • 559
2

This worked fine for me!

 <?php
     echo '
          <!DOCTYPE HTML>
          <html lang="en-US">
          <head>
              <meta charset="utf-8" />
              <link rel="stylesheet" type="text/css" href="styles.css" />
              <script type="text/javascript" src="js/jquery.js"></script>
              <script type="text/javascript" src="js/java.js"></script>
          </head>    
          <body>
              <div id="wrap">
                  <div id="main_home"></div>
                  <div id="nav_circle"></div>
                  <input type="text" id="search_text" value="Your search starts here">
              </div>
          </body>
          </html>
    ';
?>

You can echo large amount of HTML code like this in PHP. You have to be sure that is no ' inside HTML. Replace it with " and it will go on!!

aleXela
  • 1,270
  • 2
  • 21
  • 34
0

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

Traveling Tech Guy
  • 27,194
  • 23
  • 111
  • 159
  • 1
    Isn't this micro-micro-optimization? With very little actual effect? – Pekka Feb 27 '10 at 15:44
  • You might be right. Then again, it all depends on the length of the string and how the compiler manages string concatenations. E.g.: in ASP (not ASPX) the compiler would treat a & b as create a new string and put the values of string a and string b in it. Therefore a & b & c & d yielded 3-4 memory allocations etc. I'd like to hope the PHP compiler is smarter - but I'm not holding my breath :) – Traveling Tech Guy Feb 27 '10 at 16:20
0

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
  • 1,879
  • 17
  • 18
  • 2
    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 Feb 27 '10 at 16:03
  • Thanks for that link, it's a really great website :D – Daan Feb 27 '10 at 16:55