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.

<!DOCTYPE html>
<html>
<head>
<title>How to put PHP in HTML – foreach 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>

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:

<!DOCTYPE html>
<html>
<head>
<title>How to put PHP in HTML – foreach 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 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 <?php as a starting tag everywhere. In fact, PHP comes with a variation, <?=, 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.

<!DOCTYPE html>
<html>
<head>
<title>How to put PHP in HTML – Simple Example</title>
</head>
<body>
<h1>
<?= “This message is from server side.” ?>
</h1>
</body>
</html>

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.

<!DOCTYPE html>
<html>
<head>
<title>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.

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.

0
Would love your thoughts, please comment.x
()
x