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

1 Answers
Answered by suresh

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

In PHP, include(), include_once(), require(), and require_once() are all used to include and evaluate a specified file in a PHP script. However, there are key differences between them:

  • include(): Includes and evaluates the specified file during script execution. If the file is not found, a warning is issued, but the script will continue to execute.
  • include_once(): Similar to include(), but ensures that the specified file is included only once in the script. If the file has already been included, it will not be included again.
  • require(): Similar to include(), but if the specified file is not found, a fatal error is issued and script execution is halted.
  • require_once(): Similar to require(), but ensures that the specified file is included only once in the script.

It is generally recommended to use require() or require_once() when including files that are essential for the script to run properly, as they provide better error handling compared to include() and include_once().

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