How to insert PHP code in HTML document using include or require keyword ?

Inserting PHP code in an HTML document involves embedding PHP scripts within HTML files. This is done using PHP tags (`<?php … ?>`). Common methods include using include or require statements to incorporate external PHP files, allowing dynamic content within the HTML structure.

We can insert any PHP file into the HTML code by using two keywords that are ‘Include’ and ‘Require’.

Table of Content

  • PHP include() function
  • PHP require() function
  • Difference between the include() function & require() function

PHP include() function:

This function is used to copy all the contents of a file called within the function, text wise into a file from which it is called. This happens before the server executes the code.

Syntax:

include 'php filename';

Example 1: Consider a PHP file name ‘natural.php’ which contains the following code.

natural.php
<?php
  $i = 1;
  echo "the first 10 natural numbers are:";
  for($i = 1; $i <= 10; $i++) { 
    echo $i;
  }
?>

Output:

Example: Insert the above code into an HTML document by using the include keyword, as shown below.

PHP
<!DOCTYPE html>
<html>
<head>
    <title>inserting PHP code into html</title>
</head>
<body>
   <h2>Natural numbers</h2>
   <?php include 'natural.php'?>
</body>
</html>

PHP require() function:

The require() function performs same as the include() function. It also takes the file that is required and copies the whole code into the file from where the require() function is called.

Syntax:

 require 'php filename' 

Example 2: We can insert the PHP code into HTML Document by directly writing in the body tag of the HTML document.

PHP
<!DOCTYPE html>
<html>
<head>
    <title>inserting php code into html </title>
</head>
<body>
   <h2>natural numbers</h2>
<?php
  $i = 1;
  echo"the first 10 natural numbers are:";
  for($i = 1; $i <= 10; $i++) {
    echo $i;
  }
?>
</body>
</html>

Output:

natural numbers
the first 10 natural numbers are:12345678910

Example: To insert the above code into an HTML document by using the ‘require’ keyword as shown below.

PHP
<!DOCTYPE html>
<html>
<head>
   <title>inserting PHP code into html</title>
</head>
<body>
   <h2>Natural numbers</h2>
   <?php require 'natural.php'?>
</body>
</html> 

Output:

Natural numbers
the first 10 natural numbers are:12345678910

Difference between the include() function & require() function:

Include() FunctionRequire() Function
1. Includes a file and continues script execution even if the file is not found or fails to include.1. Stops script execution and generates a fatal error if the file is not found or fails to include.
2. Commonly used for optional files or modules.2. Typically used for essential files or modules critical for script functionality.
3. Failure to include a file results in a warning, but script execution continues.3. Failure to include a file results in a fatal error, halting script execution.
4. Suitable for integrating non-essential components like optional templates or modules.4. Ideal for integrating essential components like configuration files or libraries.
5. Less strict error handling, allowing script execution to proceed even if an included file is missing.5. More strict error handling, ensuring script execution halts if a required file is missing or fails to include.

Contact Us