W3docs

How do I add PHP code/file to HTML(.html) files?

To add PHP code to an HTML file, you will need to use PHP tags.

To add PHP code to an HTML file, you will need to use PHP tags. You can do this by enclosing your PHP code in <?php and ?> tags.

For example:

Example of adding PHP code to an HTML file

<!DOCTYPE html>
<html>
  <head>
    <title>My PHP Page</title>
  </head>
  <body>
    <?php
		echo "Hello, World!";
	?>
  </body>
</html>

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

Note that you will also need to save the file with a .php extension, rather than .html, in order for the PHP code to be executed by the server.

Alternatively, you can include a PHP file in an HTML file using the include or require function. For example:

How to add PHP code to an HTML file?

<!DOCTYPE html>
<html>
  <head>
    <title>My PHP Page</title>
  </head>
  <body>
    <?php include 'header.php'; ?>
    <p>Some content</p>
    <?php include 'footer.php'; ?>
  </body>
</html>

This will include the contents of the header.php and footer.php files at the respective locations in the HTML file.