Learning PHP for WordPress Development: How to Include PHP in HTML

how to include php in html

Welcome to this article on how to use PHP in HTML! Here, we try to get specific about exactly how PHP and HTML interact, at the level of a specific .php file. In other words, how do you actually include PHP in HTML, and what rules can and can’t you follow in weaving PHP and HTML together?

This article is part of our series explaining the basics of PHP for WordPress development, and builds on knowledge from two previous articles:

  1. PHP’s echo. echo is how PHP outputs things to the page. For the purposes of this article, it’s basically PHP’s “turn this into HTML” button.
  2. PHP functions. Functions are units of work that only run when they are called (invoked), and this article gets into how that sense of “control flow” affects the HTML output that is actually sent to the browser.

As a last note: to learn what works and doesn’t for using PHP in HTML, concrete examples are more helpful than theory. So the bulk of this article is examples of PHP’s proper use within HTML, with comments for each code example.

How to Include PHP in HTML: File Types and Other Considerations

By default, you can’t use PHP in HTML files, meaning files that end with .html.

The first thing to know is that, by default, you can’t use PHP in HTML files, meaning files that end with .html. It’s possible to configure your server to allow PHP in .html files, but that’s outside our scope—so for now, just remember that, if you want to write PHP, you want to be working with .php files.

In a WordPress environment, that’s largely taken care of: the entirety of WordPress is PHP-based, so you will very frequently be working with .php files, and almost never with .html files.

There’s nothing about a .php file that makes it inherently “different” than a .html file, except that it can run PHP.

The other thing you should know is this: a .php file can run exactly like a .html file, with absolutely no PHP. In other words, there’s nothing about a .php file that makes it inherently “different” than a .html file, except that it can run PHP.

A few code examples will make it clearer what we mean by this.

How .html Files Respond to HTML and PHP

Imagine we have a file, index.html, that has the following contents:

<!-- Filename: index.html -->
<h1>I am HTML markup.</h1>

Accessing this index.html file in your web browser would give you the following output: <h1>I am HTML markup.</h1>

Now what if we added the following to index.html:

<!-- Filename: index.html -->
<h1>I am HTML markup.</h1><?php echo '<p>And I am PHP.</p>'; ?>

Accessing this index.html file in your web browser would give you the following output: <h1>I am HTML markup.</h1><?php echo '<p>And I am PHP.</p>'; ?> Definitely not what we want. The issue is that, by default, HTML files don’t “speak” PHP.

How .php Files Respond to HTML and PHP

Now, what if we simply renamed index.html to index.php and ran both examples again?

<!-- Filename: index.php -->
<h1>I am HTML markup.</h1>

This would output <h1>I am HTML markup.</h1>, exactly as before. In other words, there’s no need to actually write PHP into .php files: PHP files handle plain HTML just fine.

Here’s the second example:

<!-- Filename: index.php -->
<h1>I am HTML markup.</h1><?php echo '<p>And I am PHP.</p>'; ?>

This would output the following clean HTML: <h1>I am HTML markup.</h1><p>And I am PHP.</p>

This example demonstrates that PHP files (that is, .php files) can automatically interpret PHP code—anything inside the <?php ?> tag—and turn the resulting output into HTML.

Basic Use of PHP in HTML

Here are the basics of how to include PHP in HTML. This relies on knowledge of PHP’s echo statement, which we’ve covered in a previous article.

Printing HTML Content with PHP’s echo

To output HTML content within PHP, echo it:

<div><?php echo 'Hello'; ?></div>

Output to browser will be: <div>Hello</div>

Outputting HTML Tags Using PHP

You can use PHP to output HTML tags into the page’s markup:

<div><?php echo '<p>Hello</p>'; ?></div>

Output to browser will be: <div><p>Hello</p></div>

Using PHP Inside HTML Tags

PHP can go anywhere, including inside HTML tag declarations, and including inside quotes ("):

<div class="<?php echo 'big-element'; ?>">Hello</div>

Output to browser will be: <div class="big-element">Hello</div>

Details on Opening and Closing the PHP Tag in HTML

This section relies on a basic understanding of PHP functions, which we’ve covered in an earlier article.

Opening and Closing the PHP Tag

You can swap between HTML and PHP at any time by opening (<?php) and closing (?>) the PHP tag:

<?php echo '<p>I came from PHP.</p>' ?><p>And I came from HTML.</p><?php echo '<p>Back to PHP and '; ?>now HTML.</p>

Output to browser will be: <p>I came from PHP.</p><p>And I came from HTML.</p><p>Back to PHP and now HTML.</p>

Line Breaks in PHP Code

Line breaks (as well as spaces and indentation) can work any way within the PHP tag:

<?php

echo '<p>I am PHP.</p>';

echo '<p>Still PHP.</p>';

?><p>Now HTML.</p>

Output to browser will be: <p>I am PHP.</p><p>Still PHP.</p><p>Now HTML.</p>

HTML Inside PHP Operators

HTML can go inside PHP operators of all kinds–such as if()-statements and functions, and will simply print like echo when the relevant lines of code are run.

<?php function print_greeting() { ?>
	<p>HTML output from inside function.</p>
<?php } ?>

<?php print_greeting(); ?>

Output to browser will be <p>HTML output from inside function.</p>

You can close a PHP tag, and revert to plain HTML, inside a function definition, if()-statement, or other PHP operator.

Another way to describe this code example is to note that you can close a PHP tag, and revert to plain HTML, inside a function definition, if()-statement, or other PHP operator.

Those lines of HTML will print to the page when they are executed, which depends on the control flow of the PHP logic on the page.

Declarations from Previous <?php ?> Tags Are Still Stored

PHP will remember variables, functions, and other declarations from previously opened and closed PHP tags higher on the page:

<?php function print_greeting() { ?>
	<p>Hello from function.</p>
<?php } ?>

<div>Plain HTML between two PHP tags.</div>

<?php $my_string = '<p>Hello from variable.</p>'; ?>

<div>Plain HTML between two PHP tags again.</div>

<?php print_greeting();
echo $my_string; ?>

Output to browser will be: <div>Plain HTML between two PHP tags.</div><div>Plain HTML between two PHP tags again.</div><p>Hello from function.</p><p>Hello from variable.</p>

Controlling HTML Output with PHP Operators

PHP can control the logic flow across the page, altering output.

Iterating HTML Output Using a while() Loop

Here’s an example that iterates (repeats) HTML output using a PHP while() loop:

<p>
$i = 0; // $i starts out at 0
// This while()-loop will keep running as long as $i is less than 3
<?php while( $i < 3; ) : ?>
	<span>Hello</span>
	$i++; // This means "increase $i by 1"
<?php endwhile; ?>
</p>

Output to browser will be: <p><span>Hello</span><span>Hello</span><span>Hello</span></p>

Inserting Dynamic Values into HTML Using PHP

Inserting dynamic values—values that are not pre-defined, but change as variables—into HTML using a PHP while() loop:

<p>
<?php while( $i = 0; $i < 3; $i++ ) : ?>
	<span>Number <?php echo $i; ?></span>
<?php endwhile; ?>
</p>

Output to browser will be: <p><span>Number 0</span><span>Number 1</span><span>Number 2</span></p>

This basic pattern—dynamic HTML output within a PHP while loop—is shared by WordPress’s content engine, the Loop.

Controlling HTML Output Using PHP Conditionals

You can control HTML output using PHP conditionals (if()-statements):

<?php if( 1 === 1 ) { ?>
	<p>Now you see me</p>
<?php } ?>

<?php if( 1 === 2 ) { ?>
	<p>Now you don't</p>
<?php } ?>

Output to browser will be: <p>Now you see me</p>

In the example above, we asked two things, one which is always true and one which is always false:

  1. “Does 1 equal 1?” (This is always true, so the code inside its if statement will always run, printing that HTML to the page.)
  2. “Does 1 equal 2?” (This is never true, so the code inside its if statement will never run, and its HTML does not print to the page.)

And That’s Our PHP-in-HTML Primer

Hopefully these code examples have given you a good sense of some of the basics of how to add PHP to HTML, and how PHP and HTML interact in practice.

Thanks for reading!


0 Comments
Inline Feedbacks
View all comments