How to include content of a PHP file into another PHP file ?


There are multiple ways to include the content of a PHP file into another PHP file:

  • include(): The include() function is used to include a PHP file into another PHP file. If the file is not found, it will generate a warning message but the script will continue to execute.

Example:

<?php
include('file.php');
?>
  • require(): The require() function is similar to include() but if the file is not found, it will generate a fatal error and stop the script execution.

Example:

<?php
require('file.php');
?>
  • include_once(): The include_once() function is used to include a PHP file only once. If the file has already been included, it will not be included again.

Example:

<?php
include_once('file.php');
?>
  • require_once(): The require_once() function is similar to include_once() but if the file is not found, it will generate a fatal error and stop the script execution.

Example:

<?php
require_once('file.php');
?>

In all of the above examples, "file.php" is the name of the PHP file that you want to include.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.