Broadly speaking, when it comes to using PHP in HTML, there are two different approaches. The first is to embed the PHP code in your HTML file itself with the .html extension—this requires a special consideration, which we’ll discuss in a moment. The other option, the preferred way, is to combine PHP and HTML tags in .php files.

Since PHP is a server-side scripting language, the code is interpreted and run on the server side. For example, if you add the following code in your index.html file, it won’t run out of the box.

<!DOCTYPE html>
<html>
<head>
<title>Embed PHP in a .html File</title>
</head>
<body>
<h1><?php echo "Hello World" ?></h1>
</body>
</html>

First of all, don’t worry if you haven’t seen this kind of mixed PHP and HTML code before, as we’ll discuss it in detail throughout this article. The above example outputs the following in your browser:

1<?php echo "Hello World" ?>

So as you can see, by default, PHP tags in your .html document are not detected, and they’re just considered plain text, outputting without parsing. That’s because the server is usually configured to run PHP only for files with the .php extension.

If you want to run your HTML files as PHP, you can tell the server to run your .html files as PHP files, but it’s a much better idea to put your mixed PHP and HTML code into a file with the .php extension.

That’s what we’ll show you in this tutorial.

How to Add PHP Tags in Your HTML Page

When it comes to integrating PHP code with HTML content, you need to enclose the PHP code with the PHP start tag <?php and the PHP end tag ?>. The code wrapped between these two tags is considered to be PHP code, and thus it’ll be executed on the server side before the requested file is sent to the client browser.

Let’s have a look at a very simple example, which displays a message using PHP code. Create the index.php file with the following contents under your document root.

<!DOCTYPE html>
<html>
<head>
<title>How to put PHP in HTML - Simple Example</title></head>
<body>
<h1><?php echo "This message is from server side." ?></h1></body>
</html>

The important thing in the above example is that the PHP code is wrapped by the PHP tags.

The output of the above example looks like this:

Example Output

And, if you look at the page source, it should look like this:

page source code

As you can see, the PHP code is parsed and executed on the server side, and it’s merged with HTML before the page is sent to the client browser.

Let’s have a look at another example:

<!DOCTYPE html>
<html>
<head>
<title>How to put PHP in HTML- Date Example</title>
</head>
<body>
<div>This is pure HTML message.</div>
<div>Next, we’ll display today’s date and day by PHP!</div><div>Today’s date is <b><?php echo date('Y/m/d') ?></b> and it’s a <b><?php echo date(‘l’) ?></b> today!</div><div>Again, this is static HTML content.</div>
</body></html>

This will output the current date and time, so you can use PHP code between the HTML tags to produce dynamic output from the server. It’s important to remember that whenever the page is executed on the server side, all the code between the <?php and ?> tags will be interpreted as PHP, and the output will be embedded with the HTML tags.

In fact, there’s another way you could write the above example, as shown in the following snippet.

<!DOCTYPE html>
<html>
<head>
<title>How to put PHP in HTML- Date Example</title>
</head>
<body>
<div>This is pure HTML message.</div>
<div>Next, we’ll display today’s date and day by PHP!</div><div><?phpecho 'Today’s date is <b>' . date('Y/m/d') . '</b> and it’s a <b>'.date('l').'</b> today!';?>
</div><div>Again, this is static HTML content.</div>
</body>
</html>

In the above example, we’ve used the concatenation feature of PHP, which allows you to join different strings into one string. And finally, we’ve used the echo construct to display the concatenated string.

The output is the same irrespective of the method you use, as shown in the following screenshot.

Text output of the PHP code

And that brings us to another question: which is the best way? Should you use the concatenation feature or insert separate PHP tags between the HTML tags? I would say it really depends—there’s no strict rule that forces you to use one of these methods. Personally, I feel that the placeholder method is more readable compared to the concatenation method.

The overall structure of the PHP page combined with HTML and PHP code should look like this:

<!DOCTYPE html>
<html>
<head>
<title>...</title>
</head>
<body>HTML...<?php PHP code ... ?>HTML...<?php PHP code ... ?>HTML... 
</body>
</html>
0
Would love your thoughts, please comment.x
()
x