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. You can do this by enclosing your PHP code in <?php
and ?>
tags.
For example:
<!DOCTYPE html>
<html>
<head>
<title>My PHP Page</title>
</head>
<body>
<?php
echo "Hello, World!";
?>
</body>
</html>
Watch a video course
Learn object oriented PHP
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:
<!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.