PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

In PHP, ‘include‘ and ‘require‘ are used to include and evaluate a specified file in the script. They are similar but have some important differences. Here’s a detailed explanation:

'include'

The ‘include‘ statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the ‘include‘ statement.

Syntax:

				
					include 'filename.php';

				
			

Key Points:

  • Path: The path can be relative or absolute.
  • Behavior on Failure: If the file is not found, include will produce a warning (E_WARNING), but the script will continue execution.
  • Performance: include will always include and evaluate the specified file, even if the code inside the file has already been included before.

Example:

				
					// main.php
include 'header.php';
echo 'This is the main content.';
include 'footer.php';

				
			

'require'

The ‘require‘ statement is identical to ‘include‘ except upon failure it will produce a fatal error (E_COMPILE_ERROR) and halt the script.

Syntax:

				
					require 'filename.php';

				
			

Key Points:

  • Path: Similar to include, the path can be relative or absolute.
  • Behavior on Failure: If the file is not found, require will produce a fatal error and stop the script execution.
  • Performance: require will always include and evaluate the specified file, similar to include.

Example:

				
					// main.php
require 'header.php';
echo 'This is the main content.';
require 'footer.php';

				
			

'include_once and require_once'

PHP also provides ‘include_once‘ and ‘require_once‘ statements, which are used to include a file only once.

Syntax:

				
					include_once 'filename.php';
require_once 'filename.php';

				
			

Key Points:

  • Preventing Multiple Inclusions: These statements check if the file has already been included, and if so, they do not include it again.
  • Use Case: Useful in preventing multiple function definitions, class redefinitions, or any other redundant code execution.

Example:

				
					// main.php
include_once 'header.php';
echo 'This is the main content.';
include_once 'header.php'; // This will not include header.php again

require_once 'footer.php';
echo 'More content.';
require_once 'footer.php'; // This will not include footer.php again

				
			

Practical Considerations

  • Error Handling: Use require when the file is essential for the application to run. Use include when the file is optional or when you want the script to continue running even if the file is missing.
  • Optimization: Use include_once and require_once to avoid issues with multiple inclusions and to improve script readability and maintainability.

Summary

  • include: Includes the file and continues execution if the file is not found (throws a warning).
  • require: Includes the file and halts execution if the file is not found (throws a fatal error).
  • include_once: Includes the file only once and continues execution if the file is not found (throws a warning).
  • require_once: Includes the file only once and halts execution if the file is not found (throws a fatal error).
Scroll to Top