If I can ask that answers refrain from pointing towards frameworks or other libraries, I would appreciate it. I am aware of those, and am after more of an answer based on experience for quick little hacks.
Start with some base html
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Page Title</title>
</head>
<body>
// something is going to happen here, could be more html, or php
</body>
</html>
Let’s say you will have hundreds of pages, of course, you want to modularize this in a way to have less code duplication.
I have seen, used, and debated many a tactic.
The basic includes method:
header.inc.php
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Page Title</title>
</head>
<body>
footer.inc.php
</body>
</html>
The stub of all of your hundreds of php pages:
<?php include 'header.inc.php'; ?>
// html or other php can go here
<?php include 'footer.inc.php'; ?>
Get a little more fancy, and have a var.inc.php and you could set $page_title and have that in front of the first include, saving the time of editing it out of hundreds of pages.
The functions method:
show.inc.php
function display_header($page_title) {
echo "
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>$page_title</title>
</head>
<body>";
}
You do the same for the footer, I suspect you get the idea. I know a lot of you are thinking objects at this point, which I would like to also avoid for this discussion.
Consider you are trying to teach a very young kid some principles, or that you are trying to get a designer to move into some form of the modern age.
Which either method, you start to run into cases where you are going to look at the output of the html and php in a browser. There is a very clear tab indention and structure to the source output that hits the browser. And most of the time, looking at the source of a site, I see html that is pretty abysmal. All one line, or varying forms of indenting.
How in a simple way do you solve this?
That display_header() function is going to spit in \t’s all over the place, or spaces, depending on your editor, that are going to make the rendered output a mess.
There is this option:
function display_header($page_title) {
echo '<html>';
echo '<head>';
echo "\t" . '<meta http-equiv="Content-type" content="text/html; charset=utf-8" />';
echo "\t" . '<title>' . $page_title . '</title>';
echo '</head>';
echo '<body>';
}
Then, you get into cases of:
echo '<table>';
foreach ($variable as $item) {
echo '<tr><td>' . $item . '</td></tr>';
}
echo '<table>';
versus
echo "\t" . '<table>' . "\n";
foreach ($variable as $item) {
echo "\t\t". '<tr><td>' . $item . '</td></tr>' . "\n";
}
echo "\t" . '<table>' . "\n";
And now, you have just really made a mess of your php. So I start thinking output buffers, and I am going to totally freak the designer out with that concept. But I have just mixed more html with php that I would be proud of.
And I honestly have never been able to settle up with this stuff:
<?php foreach ($variable as $item) { ?>
....
<?php } ?>
Both yields much better generated html, but to me, it also yields a more complex php file. Pretty basic question, I am sure there will be some strong opinions, and a desire to push towards frameworks and OOP. I understand those methods, but again, am looking for best suggestions looking at giving some tools to a person who is never going to have a passion for programming, and just wants to get stuff done, in a way that will help them be efficient.
Forget about the indentation of HTML output when writing PHP. Otherwise you’re spending far too much time worrying about something completely trivial.
If you need to clean up the HTML output to make it easier for a designer to read, run it through Tidy. If this seems inefficient, remember you only need to do this in development, not in production.