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:
- Use PHP delimiters to denote the HTML and PHP parts –
<p><?php $foo = "bar"; echo $foo; ?></p>
- Directly output HTML –
echo "<p>FOO</p>";
- 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!
TABLE OF CONTENTS
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
EXAMPLE CODE DOWNLOAD
Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
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.
TUTORIAL VIDEO
1) PHP DELIMITERS
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;
Start the script with <?php
and end it with ?>
, this is the very first thing we learn in PHP as a beginner. But have you ever wondered what happens when we put some text before or after the PHP tags? The answer is simple – Whatever 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 – PHP will only process the parts that are enclosed in
<?php ?>
. These are called “PHP delimiters”. - It is OK to omit the last
?>
if nothing else follows.
2) PHP DELIMITERS WITH HTML
<?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 of “HTML is a special programming language”.
- HTML is not a programming language, it is literally “plain text with formatting”.
- Whatever HTML is not enclosed within
<?php ?>
will output as-it-is.
3) SHORT ECHO TAGS
<?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
<?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
a string of HTML.
5) INCLUDE & REQUIRE HTML FILE
5A) THE HTML PAGE
<?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.
5B) THE HTML TEMPLATE
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
</head>
<body>
</body>
</html>
No need to be confused now. We literally split the HTML into the top and bottom halves, and use them as templates.
EXTRAS
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, terminate the script if the file is not found. | Click Here |
RECOMMENDED READS
- PHP Delimiters – Code Boxx
- PHP Escape Sequences – Code Boxx
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!