What are the ways to include file in PHP?

PHP allows you to include file so that page content can be reused again. There are two ways to add the file in PHP.

  1. include
  2. require

In PHP, there are several ways to include files:

  1. 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'; ?>
  2. 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'; ?>
  3. 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'; ?>
  4. 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.