Can html and php work together?

HTML is the language of the web. It is a markup language which means that the only thing we can use it for is to "markup" documents, i.e. design how content will look to the end user.

Imagine we had a page that showed the user the date.

We could use some HTML to do that:

Sunday 26 August 2012

But say we wanted to keep that page up to date. We'd have to go and manual change the date manually everyday. Because HTML is static, it can't be changed dynamically.

Perhaps it would be useful to be able to generate automatically adding the correct date to the page, depending on when the page is loaded.

That is where PHP comes in. PHP is a scripting language, and while it can be used for lots of things, one of its main uses is to generate HTML dynamically. So instead of writing in today's date - what we could do is use some PHP and say.

This will print out for me "Sunday 26 August 2012" today, "Monday 27 August 2012" tomorrow, and so on.

I'd need to save this new version of my page as page.php instead of page.html, because I need my server [which is set up use the PHP] to send the page to PHP interpreter. It will look for the special

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 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 . 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.



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 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:



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 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.



How to put PHP in HTML- Date Example


This is pure HTML message.
Next, we’ll display today’s date and day by PHP!
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 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:



...


HTML...

HTML...

HTML...  


In the next section, we’ll see how you could use PHP loops with 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 an array of items and generate output.

In most 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 at 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 at 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 section, we’ll see how you could use PHP short tag syntax.

How to Use PHP Short Tags

In the examples we’ve discussed so far, we’ve used the 

As you can see, we can omit the echo or print construct while displaying a value by using the shorthand syntax. The shorthand 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!

Including Code from Different Files

There are a lot of situations where you need to use the same code on multiple pages of a website. One such example would be the header and footer section of a website. These sections usually contain the same HTML throughout the website.

Think of this like moving the common CSS rules of a website into a stylesheet instead of placing them inside the style tags on individual pages.

There are four functions available in PHP to help you include other files within a PHP file. These are include[]include_once[]require[], and require_once[].

The include[] function will include and evaluate the specified file and give you a warning if it cannot find the file. The require[] function does the same thing, but it gives you an error instead of a warning if the file cannot be found.

When working on big projects, you might unintentionally include the same file multiple times. This could cause problems like function redefinition. One way to avoid these issues is to use the include_once[] and require_once[] functions in PHP.

Let's use code from a previous section to show you how to use these functions. I will be using include[] in this example. Create a file called header.php and place the following code inside it.



How to put PHP in HTML


This is pure HTML message.
Next, we’ll display today’s date and day using PHP!

Create another file called date.php and place the following code in it.

Create one more file called day.php and place the following code in it.

Notice that we have included the path to header.php at the top of both day.php and date.php. Make sure that the three files are in the same directory. Opening up date.php in the browser should now show you the following output.

Opening up day.php should show you the following output.

As you can see, the code we put inside header.php was included in both our files. This makes web development much easier when you are working with a lot of files. Just make the changes in one place, and they will be reflected everywhere.

Conclusion

Today, we discussed how you can mix PHP and HTML to create dynamic HTML. We discussed different methods, with a handful of examples to see how things work.

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 one of these high-quality PHP scripts 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.

Did you find this post useful?

Software Engineer, FSPL, India

I'm a software engineer by profession, and I've done my engineering in computer science. It's been around 14 years I've been working in the field of website development and open-source technologies. Primarily, I work on PHP and MySQL-based projects and frameworks. Among them, I've worked on web frameworks like CodeIgnitor, Symfony, and Laravel. Apart from that, I've also had the chance to work on different CMS systems like Joomla, Drupal, and WordPress, and e-commerce systems like Magento, OpenCart, WooCommerce, and Drupal Commerce. I also like to attend community tech conferences, and as a part of that, I attended the 2016 Joomla World Conference held in Bangalore [India] and 2018 DrupalCon which was held in Mumbai [India]. Apart from this, I like to travel, explore new places, and listen to music!

How PHP and HTML interact with each other?

PHP processor scans the page, line by line. It build a processed HTML page. If it finds HTML, it passes that on as part of the processed HTML page it is building. If it finds PHP scripts, the PHP processor may or may not output HTML.

Why PHP is not working in HTML?

The answer is in fact so simple you would want to bang your head: Simply change the file extension from ". html" to ". php"!!! Remember that you can build a webpage entirely out of PHP and all JavaScript and stuff built off JavaScript like, JQuery, bootstrap, etc will work.

When should I use PHP and HTML?

PHP is used for server-side programming which will interact with databases to retrieve information, storing, email sending, and provides content to HTML pages to display on the screen. HTML is used for specifying colors, text formatting, aligning, etc. PHP is easy to learn but not as much as HTML.

Chủ Đề