I am trying to clean up and streamline my php code. I've been reading books and they all seem to have the HMTL doctype and head info as part of each php page. Up until the now, I've been inserting the HTML preamble as an include file and it appears to work fine. What do you think...? Does it matter, or is that bad practice?

This is html.php

echo '<!DOCTYPE html>
  <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>'. $title.'</title>
    <link rel="stylesheet" type="text/css" href="http://aysoarea-1f.org/AYSO_tourn_notifier/style_sheet/stylesheet.css" />
  </head>
  <body>

This is typical module's include

$title="Notify | Admin Home"; // this appears in document and on tab
 require_once 'common/html.php';

    Nothing wrong with that approach. I like to functionalize it, so that I have to supply it with the title (and perhaps description, keywords, etc can be either required or optional parameters)

    include.php:

    function htmlHead($title, $description=null, $keywords=null)
    {
    	$html = "<!DOCTYPE html>\n<html>\n<head>\n<title>".htmlspecialchars($title)."</title>\n";
    	$html .= "<meta charset='UTF-8' />\n";
    	if(!empty($description)) {
    		$html .= "<meta name='description' content='".htmlspecialchars($description)." />\n";
    	}
    	if(!empty($keywords)) {
    		$html .= "<meta name='keywords' content='".htmlspecialchars($keywords)." />\n";
    	}
    	$html .= "</head>\n<body>\n";
    	return $html;
    }
    

    Usage:

    <?php
    include 'include.php';
    echo htmlHead("Test Page", "This is only a test.");
    

      Hi NogDog,
      Very clever way to handle the HTML. Thanks!

        Write a Reply...