PHP Include and Require Files

PHP Include and Require Files

Explanation of Include and Require:

  1. include() and require():

    • Both of these statements are used to include external files into the current file.

    • include() will generate a warning if the file cannot be found, but it will continue the script execution.

    • require() will generate a fatal error and halt the script if the file cannot be found.

    • Usage:

      include("path/to/filename.php"); require("path/to/filename.php");

    Example:

    <!DOCTYPE html> <html lang="en"> <head> <title>Tutorial Republic</title> </head> <body> <?php include "header.php"; ?> <?php include "menu.php"; ?> <h1>Welcome to Our Website!</h1> <p>Here you will find lots of useful information.</p> <?php include "footer.php"; ?> </body> </html>
    • In the above example, files header.php, menu.php, and footer.php are included in the current file.

  2. Difference Between include() and require():

    • include(): Will generate a PHP warning if the file is not found but will continue executing the rest of the script.

    • require(): Will stop the script execution if the file is not found and generate a fatal error.

    Example:

    <?php require "my_variables.php"; ?> <?php require "my_functions.php"; ?> <!DOCTYPE html> <html lang="en"> <head> <title><?php displayTitle($home_page); ?></title> </head> <body> <?php include "header.php"; ?> <?php include "menu.php"; ?> <h1>Welcome to Our Website!</h1> <p>Here you will find lots of useful information.</p> <?php include "footer.php"; ?> </body> </html>

    Tip: It's recommended to use require() for files that are essential for your application, such as configuration files, database connections, or function libraries.

  3. include_once() and require_once():

    • These functions are similar to include() and require(), but they ensure that the file is only included once, even if called multiple times.

    • This is especially useful when you have functions or classes that should only be defined once, preventing conflicts or errors when files are included multiple times.

    Example:

    // my_functions.php <?php function multiplySelf($var){ $var *= $var; // multiply variable by itself echo $var; } ?> // main.php <?php // Including file require_once "my_functions.php"; // Calling the function multiplySelf(2); // Output: 4 echo "<br>"; // Including file once again require_once "my_functions.php"; // Calling the function multiplySelf(5); // Output: 25 ?>
    • The require_once function ensures that the my_functions.php file is only included once, so the function is not redefined.

  4. Best Practices:

    • Use require() for essential files: If the file is required for your script to run (e.g., configuration, database connection), always use require() to ensure it is included and executed properly.

    • Use include() for optional files: If the file is not critical (like a footer or a menu that can be omitted), use include().

    • Use include_once() and require_once() to prevent redeclaration: Especially when working with functions, classes, or other reusable code to avoid errors caused by multiple inclusions.

This approach allows for more maintainable, readable, and DRY (Don't Repeat Yourself) code, especially when dealing with large applications where certain parts (e.g., headers, footers, configurations) are shared across multiple pages.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close