include() and require() function Use in PHP

In PHP, the content of another file is used in one file. For this, PHP has given two functions.

  1. Include() Function.
  2. Required() Fucntion.

PHP program is easy to use by using them. This avoids the programmer’s time. The larger script becomes smaller. Any external php files are used in a PHP file.

PHP Include and Require Functions, Include() and Require() Function in PHP, Using require to Include Functions, PHP File Inclusion include() and require() Functions, PHP Include File include() and require()

For Example:- If the web developer has to write the same script for the header and footer on each web page. But both header and footer are created by using separate php files instead of header and footer on web page using include () or require () function.

1.Include() Function

The include () function uses the external php file in a php file.

The external php file to include in the include function has to be written with the path to the name of the file. The path is written in double (“”) or single quotes (”).

Syntax

<?php include("csharpcorner.php"); ?>

other_file.php

Source Code :

<?php
echo "other file name is Experts PHP.";
?>

Example for include() function

my_file.php

Source Code :
<?php
echo "my file name is Experts";
include ("other_file.php");
?>

Output :

my file name is Experts
other file name is Experts PHP.

2.Required() Fucntion.

The require () function is similar to the include () function.

<?php require("other_file.php"); ?>

other_file.php

Source Code :

<?php
echo "other file name is Experts PHP.";
?>

Example for require() function

my_file.php

Source Code :
<?php
echo "my file name is Experts";
require ("other_file.php");
?>

Output :

my file name is Experts
other file name is Experts PHP.