Difficulty:BeginnerLength:MediumLanguages:
In this article, I’ll show you how to use PHP code in your HTML pages. It’s aimed to PHP beginners who are trying to strengthen their grip on the world’s most popular server-side scripting language.
Again, PHP is a server-side scripting language. That means a PHP script is executed on the server, the output is built on the server, and the result is finally sent as HTML to the client browser for rendering. It’s natural to mix PHP and HTML in a script, but as a beginner, it’s tricky to know how to combine the PHP code with the HTML code.
Today, we’re going to discuss a couple of different ways you could choose from when you want to use PHP in HTML. I assume that you have a working installation of PHP so that you can run the examples provided in this article.
Different Ways to Combine PHP and HTML
Broadly speaking, when it comes to use 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.
Embed PHP in a .html File
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 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 I’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 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 by the PHP code. Create the index.php
file with the following contents under your document root.
How to put PHP in HTML - Simple Example
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 view source of the page, 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:
How to put PHP in HTML- Date Example This is pure HTML message.Next, we’ll display today’s date and day by PHP!Today’s date is and it’s a today!Again, this is static HTML content.
This will output the current date and time, so as you can see you can use PHP code in 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 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.
How to put PHP in HTML- Date Example This is pure HTML message.Next, we’ll display today’s date and day by PHP!' . date('Y/m/d') . ' and it’s a '.date('l').' today!'; ?>Again, this is static HTML content.
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 same irrespective of the method you use as shown in the following screenshot.

And that brings another question: which is the best way? Should you use the concatenation feature or insert separate PHP tags in between the HTML tags? I would say, it really depends, there’s no strict rule which 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:
... HTML... HTML... HTML...
How to Use PHP Loops in Your HTML Page
Iterating through the arrays to produce HTML content is one of the most common tasks you'll encounter while writing PHP scripts. In this section, we’ll see how you could iterate through the an array of items and and generate output.
In most of the cases, you’ll need to display array content which you’ve populated from the database or some other sources. In this example, for the sake of simplicity, we’ll initialize the array with different values in the beginning of the script itself.
Go ahead and create a PHP file with the following contents.
How to put PHP in HTML - foreach Example List of Employees
Firstly, we’ve initialized the array in the beginning of our script. Next, we’ve used the foreach
construct to iterate through the array values. And finally, we’ve used the echo
construct to display the array element value.
And the output should look like this:

The same example with a while
loop looks like this:
How to put PHP in HTML - foreach Example List of Employees
And the output will be the same. So that’s how you can use foreach
and while
loops to generate HTML content based on PHP arrays.
In the next and last section, we’ll see how you could use PHP short tags syntax.
How to Use PHP Short Tags
In the examples we’ve discussed so far, we’ve used the as a starting tag everywhere. In fact, PHP comes with a variation of that
=
which you could use as a short-hand syntax when you want to display a string or value of the variable.
Let’s revise the example with the short-hand syntax which we discussed earlier.
How to put PHP in HTML - Simple Example = "This message is from server side." ?>
As you can see, we can omit the echo
or print
construct while displaying a value by using the short-hand syntax. The short-hand syntax is short and readable when you want to display something with echo
or print
.
So these are different ways you can use to add PHP in HTML content. As a beginner, you can learn from trying different ways to do things, and it's fun too!
Conclusion
Today we discussed how you can mix PHP and HTML to create dynamic HTML. We discussed different methods with a few handful of examples to see how things work.
Feel free to use the feed below to ask if you’ve any queries and I’ll be happy to answer them!
The Best PHP Scripts on CodeCanyon
Explore thousands of the best PHP scripts ever created on CodeCanyon. With a low-cost one time payment, you can purchase these high-quality WordPress plugins and improve your website experience for you and your visitors.

Here are a few of the best-selling and up-and-coming PHP scripts available from CodeCanyon for 2020.
35 comments
Hi there Dear, are you genuinely visiting this site on a regular basis, if so
afterward you will definitely obtain fastidious knowledge.
Wow, wonderful blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of
your web site is wonderful, as well as the content!
It’s an amazing paragraph in support of all the
web people; they will take benefit from it I am sure.
I all the time used to read article in news papers but now
as I am a user of internet thus from now I am
using net for articles, thanks to web.
Heya i’m for the first time here. I came across this board
and I find It truly useful & it helped me out much. I hope to give something
back and help others like you helped me.
It’s an awesome piece of writing in favor of all the online visitors; they will take advantage
from it I am sure.
I really love your site.. Pleasant colors & theme.
Did you make this site yourself? Please reply back as I’m attempting to create
my own personal blog and want to find out where you got this from or just what the theme is called.
Many thanks!
What’s up, after reading this awesome paragraph i am too
delighted to share my experience here with mates.
Nice post. I learn something totally new and challenging
on websites I stumbleupon every day. It’s always
helpful to read content from other writers and practice something from other websites.
Hello! This is my 1st comment here so I just wanted to give
a quick shout out and say I genuinely enjoy reading through your
posts. Can you recommend any other blogs/websites/forums that deal with the same topics?
Thanks a ton! asmr https://app.gumroad.com/asmr2021/p/best-asmr-online asmr
Hello my friend! I wish to say that this article is awesome,
great written and include almost all important infos.
I’d like to peer more posts like this . quest bars http://j.mp/3jZgEA2 quest bars
This is a really good tip particularly to those new
to the blogosphere. Simple but very precise information… Thank you for sharing this one.
A must read post! scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery
As the admin of this web page is working, no hesitation very rapidly it will be
well-known, due to its feature contents. cheap flights http://1704milesapart.tumblr.com/ cheap flights
I believe this is one of the so much vital info for me.
And i’m glad studying your article. However wanna statement on some common issues, The
web site taste is ideal, the articles is really great
: D. Good process, cheers scoliosis surgery https://0401mm.tumblr.com/ scoliosis surgery
Thanks a bunch for sharing this with all of us you actually realize what you’re talking approximately!
Bookmarked. Please also talk over with my website =). We will have a hyperlink exchange arrangement
between us ps4 games https://tinyurl.com/45xtc52b ps4
Why visitors still make use of to read news papers when in this technological world the whole thing is
accessible on net? quest bars https://www.iherb.com/search?kw=quest%20bars quest
bars
Hi there! I know this is somewhat off topic but I was wondering if you knew
where I could locate a captcha plugin for my comment form?
I’m using the same blog platform as yours and I’m
having trouble finding one? Thanks a lot! part time jobs hired in 30 minutes https://parttimejobshiredin30minutes.wildapricot.org/
Hello, yeah this article is really fastidious and I have learned lot
of things from it on the topic of blogging.
thanks.
Hi I am so grateful I found your website, I really found you by mistake, while I was searching on Digg for something
else, Anyhow I am here now and would just like to say thanks for a remarkable post and a all round exciting blog (I also love the
theme/design), I don’t have time to browse it all at the moment but I have bookmarked
it and also added in your RSS feeds, so when I have time I will be back to read
a lot more, Please do keep up the fantastic jo.
What’s up to every one, the contents existing at this
web site are actually awesome for people knowledge, well,
keep up the good work fellows.
Thank you for the auspicious writeup. It in truth
used to be a amusement account it. Glance advanced to
more introduced agreeable from you! However, how could we be in contact?
With havin so much content and articles do you ever run into any problems
of plagorism or copyright infringement? My site has a lot of exclusive content I’ve either written myself or outsourced but it
seems a lot of it is popping it up all over the web without my authorization. Do you know any solutions to help reduce content from being
stolen? I’d certainly appreciate it.
I know this web site gives quality dependent articles or reviews and additional information, is there any other site
which gives such data in quality?
Thanks for the marvelous posting! I definitely enjoyed reading it, you can be a great
author.I will be sure to bookmark your blog and will come back in the foreseeable future.
I want to encourage you to ultimately continue your great work, have a nice weekend!
My partner and I stumbled over here different page and thought I should check things out.
I like what I see so now i’m following you. Look forward
to finding out about your web page for a second time.
Howdy just wanted to give you a brief heads up and let you know a few of the pictures aren’t loading correctly.
I’m not sure why but I think its a linking issue. I’ve tried it in two different
web browsers and both show the same outcome.
Greetings! Very useful advice within this article!
It’s the little changes which will make the most important changes.
Thanks for sharing!
In fact when someone doesn’t be aware of after that
its up to other visitors that they will assist, so here it takes place.