What is the difference between include(), require(), include_once() and require_once() functions in PHP?

1 Answers
Answered by suresh

The Difference Between include(), require(), include_once() and require_once() functions in PHP

When it comes to PHP, there are important functions that allow you to include and require files within your code. Let's delve into the distinctions between include(), require(), include_once() and require_once() in PHP:

include(): The include() function includes and evaluates a specified file during the execution of the script. If the file is not found, a warning is generated but the script will continue to execute.

require(): The require() function is similar to include() but has one key difference – if the specified file cannot be found, a fatal error will be produced, and the script execution will halt.

include_once(): This function ensures that the specified file is included only once during the script execution, even if it is referenced multiple times.

require_once(): Like include_once(), require_once() includes the specified file only once but, similar to require(), it will produce a fatal error if the file is not found.

In summary, the primary difference is the behavior when the specified file is not found – include() and include_once() generate warnings while require() and require_once() create fatal errors. Choose the function to use based on whether the included file is essential for the script execution.

Answer for Question: What is the difference between include(), require(), include_once() and require_once() functions in PHP?