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.
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:
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.
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:
And, if you look at the page source, it should look like this:
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:
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
?> 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.
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.
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: