Outputting an HTML document from PHP

From Knowledge Kitchen
Jump to navigation Jump to search


How not to output an HTML document

You are forbidden from ever doing this and you should never take a class taught by someone who does this, even though it works:

 1 <?php
 2 
 3 print(" <!DOCTYPE html>");
 4 print(" <html lang=\"en\">");
 5 print("      <head>");
 6 print("           <meta charset=\"utf-8\" />");
 7 print("           <title>Insert the title of your page here</title>");
 8 print("      </head>");
 9 print("      <body>");
10 print("           Insert the contents of your page here");
11 print("               <table>");
12 print("               </table>");
13 print("     </body>");
14 print(" </html>");
15 
16 ?>

How to output an HTML document

To output HTML, don't use PHP at all! Anything not between the opening and closing PHP tags (i.e. <?php and ?>) will be output without requiring PHP.

 1 <!DOCTYPE html>
 2 <html lang="en">
 3      <head>
 4           <meta charset="utf-8" />
 5           <title>Insert the title of your page here</title>
 6      </head>
 7      <body>
 8           Insert the contents of your page here
 9     </body>
10 </html>

How to output HTML with embedded PHP

Sometimes you need PHP.

  • Use the PHP script to output the text on the page, but try to avoid using PHP to output HTML tags.
  • Use small self-contained blocks of PHP embedded within the HTML. Everything not within the PHP blocks will be output as plain text.
 1 <!DOCTYPE html>
 2 <html lang="en">
 3      <head>
 4           <meta charset="utf-8" />
 5           <title><?php print("This is my page title!"); ?></title>
 6      </head>
 7      <body>
 8 
 9           <h1><?php print("This is my heading!!!!"); ?></h1>
10 
11           <p>
12               <?php print("This is the content of my page!"); ?>
13           </p>
14 
15     </body>
16 </html>

How to take advantage of PHP's alternative syntax

If you need to use control structures, like if statements or loops, use PHP's alternative syntax to embed them within the HTML code with minimal disruption.

 1 <!DOCTYPE html>
 2 <html lang="en">
 3      <head>
 4           <meta charset="utf-8" />
 5           <title>Example of a PHP loop in an HTML document</title>
 6      </head>
 7      <body>
 8 
 9           <h1>Example of a PHP loop in an HTML document</h1>
10 
11           <ul> 
12 
13 <?php for ($i=0; $i<10; $i++) : ?>
14 
15               <li><?php print($i); ?></li> 
16 
17 <?php endfor; ?>
18 
19           </ul> 
20            
21     </body>
22 </html>

Keeping complicated PHP logic separate from an HTML template

When your PHP code is quite complicated, such as when retrieving data from a database, only embed the parts of the PHP code that actually print something into the HTML. Do any complicated PHP stuff that is not related to printing somewhere out of the way, such as at the very top of your document or in a separate file.

 1 <?php
 2 
 3 //BEGIN COMPLICATED PHP STUFF
 4 //...note that none of this PHP code is directly related to printing HTML to the browser.
 5 
 6 error_reporting(E_ALL);
 7 ini_set("display_errors", 1);
 8 $cxn = new mysqli("my_server", "az1234", "^sa%9h4", "my_database");
 9 $query = "SELECT * FROM favorite_viking_metal_bands ORDER BY formed ASC;";
10 $result = $cxn->query($query); 
11 
12 //END COMPLICATED PHP STUFF
13 
14 ?>
15 <!DOCTYPE html>
16 <html lang="en">
17      <head>
18           <meta charset="utf-8" />
19           <title>Example of a Complicated PHP mixed with HTML</title>
20      </head>
21      <body>
22 
23           <h1>Example of a Complicated PHP mixed with HTML</h1>
24           <p>Note that the only PHP code that is embedded within the HTML is that code related to outputting HTML to the browser</p>
25 
26           <ul> 
27 
28 <?php while ($row = $result->fetch_assoc()) : ?>
29 
30               <li><?php print($row['band']); ?></li> 
31 
32 <?php endwhile; ?>
33 
34           </ul> 
35            
36     </body>
37 </html>


What links here