In terms of performance , what would be better. Using PHP to echo all the HTML output so I can pepper it with the various bits of working code and variables or escape HTML t
It's pretty irrelevant to the overall speed of your app which one you go with.
That being said, I know you said you don't care, but please use the 2nd one as it's about a million times easier to read. And readability is huge.
You should seriously consider never placing PHP inside of HTML. You are mixing logic with view which makes a project messy.
As others have said, if you output into html, use <?php echo $whatever; ?>
If you have to output a ton of information look into output buffering.
For small bits I just keep it all in PHP strings, like when looping through an array to generate a list. But for larger piles of markup I keep that out of the PHP and just drop into PHP when necessary.
This is largely because I prefer to optimize for maintainability. Not just because it's faster to see what's going on when the html has appropriate syntax highlighting applied, but because it also makes mistakes resulting from a flawed reading of the code less likely to occur.
There really isn't a best way. I just try to stay consistent with what's described above, and keep my eyes open for situations where maybe the other way would be a better way of handling it.
It falls into the realm of micro optimizations. The largest part of your time falls into initializing the PHP engine starting the work.
So unless you have an order of tens of thousands of lines (or even more), you shouldn't be concerned with it.
To add, i did a small test of a million lines where i used php to print them and where i used php to call a c program that did the same thing and the difference was minuscule.
A little more info.
What is happening in the 2nd example is that you are "turning on/off" PHP, its not exactly what is happening but for this example it fits.
The thing you should be more worried about, is that code going to have a lot of logic around it? Am i going to split that string into even more strings or even place it in different places? Is this the View of a MVC application?
For 1 and 2 it could be a toss up between either method. But for 3 i would go with method 2 for these reasons.
A view in a MVC web application is mostly html/css. So i want to see that be formatted correctly and i want to see in my editor the html coloring. So that is a plus.
I prefer to use php heredoc function, this way I don't need to escape any characters, ex:
<?php
$title = "heredoc is cool!!!";
$mySite = "http://www.php.net";
echo <<<LOL
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> $title </title>
<link rel="shortcut icon" href="$mySite/favicon.ico">
<link rel="search" type="application/opensearchdescription+xml" href="$mySite/phpnetimprovedsearch.src" title="Add PHP.net search">
<link rel="alternate" type="application/atom+xml" href="$mySite/releases/feed.php" title="PHP Release feed">
<link rel="alternate" type="application/atom+xml" href="$mySite/feed.atom" title="PHP: Hypertext Preprocessor">
<link rel="canonical" href="$mySite/manual/en/language.types.string.php">
<link rel="shorturl" href="$mySite/types.string">
<link rel="contents" href="$mySite/manual/en/index.php">
<link rel="index" href="$mySite/manual/en/language.types.php">
<link rel="prev" href="$mySite/manual/en/language.types.float.php">
<link rel="next" href="$mySite/manual/en/language.types.array.php">
LOL;
?>
Note:
Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.
In terms of performance... it's not important. Readability of the code is king, a tiny fraction of a percent of performance difference isn't going to change anything.
The raw HTML version is usually the easiest to read (and probably has the best performance too for what it's worth - but what it's worth is: nothing). This is no surprise: PHP is an HTML templating language, the whole point is interleaving HTML at a language syntax level.
Look at nickf's code to see how to keep it readable. Indenting is important! Put a level of indenting inside each PHP control structure too, so you can keep track of them. eg.:
<?php if ($error) { ?>
<p> Oh no, error! </p>
<?php } ?>
Finally, when outputting content, such as $container in your example, you must always htmlspecialchars() it, or you'll have an application full of HTML-injection security holes, like every other PHP newbie (and even many professional developers, sadly). This matters whichever method you use to output content.
Because htmlspecialchars is quite an annoyingly long function name, you could try defining your own shortcut function:
<?php
function h($s) {
echo(htmlspecialchars($s, ENT_QUOTES));
}
?>
<ul>
<?php foreach ($things as $thing) { ?>
<li> <?php h($thing['name']) ?> </li>
<?php } ?>
</ul>