PHP allows you to include file so that page content can be reused again. There are two ways to add the file in PHP.
- include
- require
In PHP, there are several ways to include files:
- include: This statement includes and evaluates the specified file. If the file is not found or errors occur during inclusion, a warning will be issued, but the script will continue execution.
php
<?php include 'filename.php'; ?>
- require: This statement includes and evaluates the specified file. If the file is not found or errors occur during inclusion, a fatal error will occur, and the script will halt execution.
php
<?php require 'filename.php'; ?>
- include_once: This statement includes and evaluates the specified file but ensures that the file is included only once during a script’s execution.
php
<?php include_once 'filename.php'; ?>
- require_once: This statement includes and evaluates the specified file but ensures that the file is included only once during a script’s execution. If the file has already been included, it will not be included again.
php
<?php require_once 'filename.php'; ?>
These methods allow you to modularize your code by separating it into different files and including them as needed. They are commonly used for including libraries, configurations, or other reusable code snippets into PHP scripts.