Home > Software design >  Whats the best way to including Header Footer in PHP?
Whats the best way to including Header Footer in PHP?

Time:10-10

If I want to include header file on a page, whats the best way to create header.php file?

Should I include this section of the html to my header.php file so it shows up on every page?

<!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Mobile</title>
      <link rel="stylesheet" href="style.css">
  </head>

Or should I only include navbar content in the header.php?

Thanks

CodePudding user response:

This is from a few years ago by Harvard's CS50 class:

function render($template, $values = array()){

   // if template exists, render it    
   if (file_exists("../views/$template")){    

      // extract variables into local scope    
      extract($values);    

      // render header_form    
      require("../views/header_form.php");    

      // render template    
      require("../views/$template");    

      // render footer    
      require("../views/footer_form.php");

   }else{

      // else err    
      trigger_error("Invalid template: $template", E_USER_ERROR);

   }    
}

make sure your header has <!DOCTYPE html><html><head></head><body> and your footer has closing </body></html>

As far as whether or not to include navigation in the header it depends on whether or not you want every page to have the same navigation

  • Related