How To Use HTML PHP On The Same Page – Simple Examples

Welcome to a quick beginner’s tutorial on how to use HTML and PHP on the same page. So you have just started with server-side programming, and wondering if it is possible to use both HTML and PHP on the same page? How do we generate or add HTML code in PHP?

We can use both HTML and PHP on the same page, in a PHP file:

  1. Use PHP delimiters to denote the HTML and PHP parts – <p><?php $foo = "bar"; echo $foo; ?></p>
  2. Directly output HTML – echo "<p>FOO</p>";
  3. Include or require an HTML file – include "PAGE.HTML"; require "PAGE.HTML";

Let us walk through a few examples to better illustrate these – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes PHP-HTML Examples Useful Bits & Links
Tutorial Video The End

 

 

DOWNLOAD & NOTES

First, here is the download link to the example source code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

PHP HTML ON THE SAME PAGE

All right, let us now move into the examples of HTML and PHP working together on the same page.

 

1) PHP DELIMITERS

1-delimiter.php
This will output as-it-is.
 
<?php
// (A) THIS PART IS PHP
$foo = "bar";
echo $foo;
?>
 
This will also output as-it-is.
 
<?php
// (B) THIS PART IS PHP AGAIN
// IT iS OK TO OMIT THE CLOSING TAG IF NOTHING ELSE FOLLOWS
$hello = "world";
echo $hello;

The very first thing we learn in PHP as a beginner, start the script with <?php and end it with ?>. But have you ever wondered what happens when we put some text before or after the PHP tags? The answer is simple – Whatever that is not enclosed within <?php ?> will output as-it-is. For those who are confused:

  • Get rid of the idea of “PHP scripts must start with <?php and end with ?>“.
  • We can use as many pairs of <?php ?> in a single page as we like.
  • The correct way to put it is – PHP will only process the parts that are enclosed in <?php ?>. This is called a “PHP delimiter”.
  • It is OK to omit the last ?> if nothing else follows.

 

 

2) PHP DELIMITERS WITH HTML

2-delimit-html.php
<?php
// (A) THIS PART IS PHP
$foo = "bar";
 
// (B) OUTPUT HTML AS-IT-IS ?>
<!DOCTYPE html>
<html>
  <head>
    <title>PHP Delimiters</title>
  </head>
  <body>
    <h1><?php
    // (C) THIS PART IS PHP AGAIN
    echo $foo;
    ?></h1>
  </body>
</html> 

This should be very straightforward now, we can “weave” between PHP and HTML using delimiters. For those who are still confused:

  • Get rid of the idea that “HTML is a special programming language”.
  • HTML is not a programming language, it is literally “text with formatting”.
  • Whatever HTML that is not enclosed within <?php ?> will output as-it-is.

 

3) SHORT ECHO TAGS

3-short-echo.php
<?php
// (A) DECLARE VARIABLE IN PHP
$foo = "bar";
?>
 
<!-- (B) THE NOT-CLEVER WAY -->
<h1><?php echo $foo; ?></h1>
 
<!-- (C) THE QUICK WAY -->
<h1><?=$foo?></h1> 

In the above example, you have seen how we can use <h1><?php echo $VARIABLE; ?></h1> to output a PHP variable into an HTML tag. The faster “shorthand” is to use the short echo tag – <h1><?=$VARIABLE?></h1>.

 

 

4) ECHO HTML

4-echo-html.php
<?php
// (A) DECLARE VARIABLE IN PHP
$foo = ["foo", "goo", "coo", "koo"];
 
// (B) OUTPUT HTML LIST
echo "<ul>";
foreach ($foo as $f) {
  echo "<li>$f</li>";
}
echo "</ul>";

For the people who are still stuck with “HTML is special code” – It is just plain text, we can even echo it.

 

5) INCLUDE & REQUIRE HTML FILE

THE HTML PAGE

5-template.php
<?php
// (A) HEADER
$title = "Random Test Page";
require "5a-top.php";
 
// (B) BODY ?>
<h1>Hello World</h1>
<p>It works!</p>
 
<?php // (C) FOOTER
include "5b-bottom.html";

What in the world is happening here? Where are the rest of the <html> <head> <body>? Isn’t require include used to load other PHP scripts? Keep calm and look carefully. There are no file type restrictions for require include, we can use them to load plain HTML files.

 

 

THE HTML TEMPLATE

5a-top.php
<!DOCTYPE html>
<html>
  <head>
    <title><?=$title?></title>
  </head>
  <body>
5b-bottom.html
  </body>
</html>

No need to be confused now, we literally split the HTML into the top and bottom halves, use them as templates.

 

USEFUL BITS & LINKS

That’s all for this tutorial, and here is a small section on some extras and links that may be useful to you.

 

SUMMARY

Syntax / Function Description Reference Link
<?php and ?> PHP delimiters – PHP will only execute statements enclosed inside these delimiters. Click Here
<?= and ?> Short open tag for outputting a variable. Click Here
echo "<p>HTML<p>" Output some text/HTML. Click Here
include("FILE") Load the specified file, throws an error but will not terminate the script if the file is not found. Click Here
require("FILE") Load the specified file, terminates the script if the file is not found. Click Here

 

RECOMMENDED READS

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

How To Add HTML Code In PHP (click to enlarge)

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *