Newsletter

require() vs include() in PHP: fatal error or warning

The require() function in PHP is essentially used to pull the content, code or data of one PHP file into another PHP file.

require() vs include() in PHP: fatal error or warning

What is the difference between require() and include()?

      include()                                             require()
The include() function does not stop the script, even when an error occurs. The require() function stops the script as soon as an error occurs.
The include() function does not raise a fatal error. The require() function raises a fatal error
The include() function is mostly used when the file is not essential and the application has to carry on with its work when the file is not found. The require() function is mostly used when the file is mandatory for the application.
The include() function only produces a warning (E_WARNING) and the script keeps running. require() produces a fatal error (E_COMPILE_ERROR) alongside the warning.

require() in PHP

The require() function in PHP is essentially used to pull the content, code or data of one PHP file into another PHP file. During that process, if anything goes wrong, the require() function displays a warning along with a fatal error and stops the script there and then. To use this require() function, we first have to create two PHP files. Using the include() function, include one PHP file inside the other. After that, you will see the two PHP files combined into a single HTML file

include() in PHP

The include() function in PHP is essentially used to pull the content, code or data of one PHP file into another PHP file. During that process, if anything goes wrong, the include() function displays a warning, but unlike the require() function, it does not stop the script: on the contrary, the script carries on with its work. To use the include() function, we first have to create two PHP files. Using the include() function, include one PHP file inside the other. After that, you will see the two PHP files combined into a single HTML file.

PHP

Damien Flandrin Web developer since 2010, creator of Gekkode and Email Impact. Every article is tested on a real project before publication. Contact
Newsletter

New tests, tutorials and projects, by e-mail.

Reproducible tests, versioned code, dated results. Never any spam.