How To Link PHP to HTML

This article is covering the problem of how to link PHP to HTML. It is hard for beginners to understand how to include PHP files in HTML. It is a tricky process to load PHP …

How To Link PHP to HTML

This article is covering the problem of how to link PHP to HTML. It is hard for beginners to understand how to include PHP files in HTML. It is a tricky process to load PHP in HTML or combine both in the same file. 

There are two distinct ways to Link PHP to Html.

  1. Use PHP code inside HTML and change file extension from .html to .php.

It is impossible to run PHP code inside HTML if the running file is saved with a .html extension. To run PHP inside HTML code, the developer needs to save the file as filename.php. In this process, PHP code is embedded in HTML, and HTML itself is enclosed with the PHP extension.

We cannot run PHP files directly. We need a server host such as XAMPP to run a PHP file in the browser. For example: Given code starts and ends with HTML but there is a small PHP code inside HTML. If you directly run this code in a browser with a .html extension, only HTML output will show. To run the PHP code in HTML, use the .php extension. Save this code file as demo.php instead of demo.html. Turn on your server to run PHP files in the browser.

<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
     <h1>This is html output</h1>
<h1>
<?php
    echo "This is PHP output";
?></h1>
</body>
</body>
</html>

Output

How To Link PHP to HTML

when we save the file with .html, the displayed output will only text that is written inside HTML tags. The output screen will not display PHP code output.

How To Link PHP to HTML

Note : we cannot run php code inside html without using the .php extension file. We need to change .html to .php to run the PHP code  inside html.

2. How to load external PHP files inside HTML

It is not necessary to write PHP code inside HTML. If you are working on a large project that contains hundreds of lines of code, then it is hard to manage one file that contains both PHP and HTML code OR if you are using the same PHP/HTML/Text file at multiple places on a website and you need that file again and again. In this situation, developers need to create separate files as modules and integrate all of them to reduce the complexity of the code. 

Here we are solving one of the problems that are commonly faced by beginners: how to embed PHP files to HTML.

There are two methods to integrate external PHP files with HTML code.

  1. include 
  2. require

It is easy to embed one PHP file into another PHP file by using these two methods. Both methods work the same but the difference between them is only that include continue to show output in case the include file is missing but require to stop to proceed more if the required file is missing. We use require when we need mandatory data from the user or for permissions and security purposes, and use the include statement when we want to add the same file on different pages of the website such as header, main menu, about us,  footer, etc. In the include statement, if you want to update one of the include files, you can update a specific include file instead of whole code such as update footer file. This means include use to include additional features and require use for mandatory data.

1. Include PHP file in HTML

create a file that contains all the text/markup inside that file and include or require statements will help to load PHP in HTML. these statement helps to load external .php file inside HTML


Create a file test.php, paste the below-provided code and save.

<?php
echo "<p><h2>This is PHP file</h2></p>";
?>

Create another file try.php, paste the below-provided code, save, and run.

<!DOCTYPE html>
<html>
<head>
<title>PHPExternalfile</title>
</head>
<body>
     <h1>This is HTML Webpage</h1>
<?php
  include 'test.php';
?>
</body>
</html>

output

How To Link PHP to HTML

The include statement will include a PHP code file by calling it filename.php.

2. require PHP file in HTML

Create a file test.php, paste the below-provided code and save.

<?php
echo "<p><h2>This is PHP file</h2></p>";
?>

Create another file try.php, paste the below-provided code, save, and run.

<!DOCTYPE html>
<html>
<head>
<title>PHPExternalfile</title>
</head>
<body>
     <h1>This is HTML Webpage</h1>
<?php
 require 'test.php';
?>
</body>
</html>

the output will display the same as above because both methods work the same if the file is existing that is called inside PHP code.

How To Link PHP to HTML

Require statement works same as to include statement but there the only distinct difference is that require is restricted to use file and cannot process further if a required PHP file is missing but include can. 

Difference between include and require statement in PHP

  • Include a statement example when the file is missing.
<!DOCTYPE html>
<html>
<head>
	<title>PHPExternalfileinHTML</title>
</head>
<body>
<h1>This is HTML Webpage</h1>
<?php include 'NoFile.php';
 echo"<h1>This is the include statement example inside $PHP $File </h1>";
?>
</body>
</html>

Output

Output: include statement generates Warnings with the output if the include file is missing but shows all the output including HTML and PHP except missing PHP file.

How To Link PHP to HTML
  • require statement example when the file is missing.
<!DOCTYPE html>
<html>
<head>
	<title>PHPExternalfileinHTML</title>
</head>
<body>
<h1>This is HTML Webpage</h1>
<?php require 'NoFile.php';
 echo"<h1>This is the require statement example inside $PHP $File </h1>";
?>
</body>
</html>

Output:

require statement generates a fatal error with the output if the require file is missing, only run HTML code and exclude PHP code.

How To Link PHP to HTML

Conclusion

In this article, we are discussing the methods of how to link PHP to HTML. We discuss how to run PHP code inside HTML code and how to externally include PHP files in HTML.

Suggested Read: Reactstrap vs. React-Bootstrap

Leave a Comment