How to Use PHP in HTML?

How to Use PHP in HTML? 10 Best Points | CIO Women Magazine

I’ll show you how to Use PHP in HTML in this article. It is for people who are just starting out with PHP and want to learn more about the most popular server-side scripting language in the world.

Again, PHP is a language for scripts that run on the server. That means a PHP script is run on the server, the output is built on the server, and the result is sent as HTML to the client browser so it can be displayed. It’s normal to mix PHP and HTML in a script, but it can be hard for a beginner to figure out how to put the PHP code with the HTML code together.

Here’s how to Use PHP in HTML;

When it comes to Use PHP in HTML, there are two main ways to do things. The first way is to put PHP code inside your HTML file, which has an extension of.html. This requires a special thought, which we’ll talk about in a moment. The other way, which is the best way, is to mix PHP and HTML tags in.php files.

PHP is a server-side scripting language, which means that the code is read and run on the server. For instance, if you put the code below in your index.html file, it won’t work right away.

First of all, don’t worry if you’ve never seen Use PHP in HTML code mixed together like this before. We’ll talk about it in detail in this article. The above example tells your browser to show the following:

<?php echo “Hello World” ?>

So, as you can see, Use PHP in HTML document are not picked up by default. They are just treated as plain text and output without being parsed. That’s because most servers are set up to run PHP only on files that end in.php.

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 in a file with the.php extension.

In this lesson, I’ll show you how to do that.

1. HOW TO ADD PHP TAGS TO YOUR HTML PAGE

When you want to Use PHP in HTML, you need to wrap it in the PHP start tag (?php) and the PHP end tag (?>). The code between these two tags is thought to be PHP code, so it will be run on the server before the requested file is sent to the browser on the client.

How to Use PHP in HTML? 10 Best Points | CIO Women Magazine

Let’s look at a very simple example of how PHP code can be used to show a message. Under your document root, write the following into the index.php file.

<!DOCTYPE html>

<html>

<head>

<title>

2. How to put PHP in HTML – Simple Example

</title>

</head>

<body>

<h1><?

php echo “This message is from server side.” ?></h1>

</body>

</html>

In the above example, what’s important is that the PHP code is wrapped in PHP tags.

This is what comes out of the above example:

Example Output

If you look at the source code of the page, it should look like this:

page source code

As you can see, the Use PHP in HTML is read and run on the server, and it is combined with HTML before the page is sent to the client browser.

Let’s look at a different example:

<!DOCTYPE html>

<html>

<head>

<title>

3. How to put PHP in HTML- Date Example

</title>

</head>

<body>

<div>

This is a pure HTML message.

</div>

<div>

Next, we’ll display today’s date and day by Use PHP in HTML!

</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 you need to use Use PHP in HTML.

</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 you need to Use PHP in HTML.

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

<!DOCTYPE html>

<html>

<head>

<title>

4. 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 Use PHP in HTML!

</div>

<div>

<?

php

echo ‘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 Use PHP in HTML, 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>

In the next section, we’ll see how you could use PHP loops with HTM

5. HOW TO USE PHP LOOPS ON YOUR HTML PAGE

One of the most common things you’ll have to do when writing PHP scripts is iterating through the arrays to get HTML content. In this section, we’ll look at how you can go through an array of items and make an output.

How to Use PHP in HTML? 10 Best Points | CIO Women Magazine

Most of the time Use PHP in HTML, you’ll need to show an array of content that you got from the database or somewhere else. In this example, we’ll start the array with different values at the beginning of the script to keep things simple.

Go ahead and write the following into a PHP file.

<!DOCTYPE html>

<html>

<head>

<title>

6. How to put PHP in HTML – for each Example

</title>

</head>

<body>

<?php

$employees = array(‘John’, ‘Michelle’, ‘Mari’, ‘Luke’, ‘Nellie’);

?>

<h1>

List of Employees</h1>

<ul>

<?php foreach ($employees as $employee) { ?>

<li><?php echo $employee ?></li>

<?php }  ?>

</ul>

</body>

</html>

First, we set up the array at the beginning of our script. Then, we used the foreach construct to go through each value in the array you need to Use PHP in HTML. We’ve used the echo construct to show the value of an array element.

And this is how the result should look:

The output that shows a list of workers

The same example with a while loop looks like this:

<!DOCTYPE html>

<html>

<head>

<title>

7. How to put PHP in HTML – for each Example

</title>

</head>

<body>

<?php

$employees = array(‘John’, ‘Michelle’, ‘Mari’, ‘Luke’, ‘Nellie’);

$total = count($employees);

?>

<h1>List of Employees

</h1>

<ul>

<?php

$i = 0;

?>

<?php while ($i < $total) { ?>

<li><?php echo $employees[$i] ?></li>

<?php  ++$i ?>

<?php }  ?>

</ul>

</body>

</html>

And the output will be the same. So that’s how you can use each and while loops to generate HTML content based on PHP arrays.

In the next section, we’ll see how you could Use PHP in HTML short tag syntax.

8. HOW TO USE PHP SHORT TAGS

In all of the examples we’ve looked at so far, we’ve used? php as the first tag. In fact, PHP has a variation called?= that can be used as a shorthand syntax to show a string or the value of a variable.

How to Use PHP in HTML? 10 Best Points | CIO Women Magazine

Let’s revise the example with the short-hand syntax which we discussed earlier.

<!DOCTYPE html>

<html>

<head>

<title>

9. How to put PHP in HTML – Simple Example

</title>

</head>

<body>

<h1><?= “This message is from server side.” ?></h1>

</body>

</html>

By using the shorthand syntax, we can show a value without using the echo or print constructs. When you want to show something on the screen with echo or print, the shorthand syntax is short and easy to read.

So, these are the different ways you can add PHP to HTML content. As a beginner, trying different ways to do things is a great way to learn and have fun.

Putting Code from Several Files Together

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 sections of a website. These sections usually contain the same HTML throughout the website you need to Use PHP in HTML.

Think of this as 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 PHP in HTML 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.

<!DOCTYPE html>

<html>

<head>

<title>

10. How to put PHP in HTML

</title>

</head>

<body>

<div>

This is pure HTML message.

</div>

<div>

Next, we’ll display today’s date and day using PHP!

</div>

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

<?php

include(‘header.php’);

?>

<div>

<?php

echo ‘Today’s date is <b>

‘ . date(‘Y/m/d’) . ‘</b>!’;

?>

</div>

</body>

</html>

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

<?php

include(‘header.php’);

?>

<div>

<?php

echo ‘Today is <b>

‘.date(‘l’).'</b>!’;

?>

</div>

</body>

</html>

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.

PHP Date Include

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

PHP Day Include

As you can see, the code we put inside header.php was included in both of 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 Use PHP in HTML.

CONCLUSION

Today, we talked about how you can combine Use PHP in HTML to make HTML that changes. We talked about different ways to do things and gave a few examples to show how things work.

Share:

LinkedIn
Twitter
Facebook
Reddit
Pinterest

Related Posts