PHP Build a Basic PHP Website (2018) Adding a Basic Form Using A Third-Party Library

coskun olcucu
coskun olcucu
5,340 Points

include,require,include once and require once

Hi,

Can someone explain more in details include and require etc?

Diar Selimi
Diar Selimi
1,341 Points

Include / Require you can include the same file more than once also: require() is identical to include() except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.

1 Answer

geoffrey
geoffrey
28,720 Points

As stated by Alena in the course here are the differences :

The difference between include and require is what happens if they encounter an error. If you use the include command and the file doesn't exist then PHP will throw a warning. But it will still execute the rest of the code. On the other hand if you use require and the file doesn't exist then PHP will error and will not execute any more code.

The other difference between them is how many times PHP will include the file. If you use include or require on a same file multiple times, it will load that file multiple times. This will work fine for HTML snippets like our header and footer files.

But with other files, like files like create new functions, this would cause an error.

So, to sum up if you use "_once", PHP will look if the file has already been loaded, and if not, It will load it.

Exemple

include('details.php');
include('details.php');
// The details page 'll be included twice.

include('details_second.php');
include_once('details_second.php');
// The details_second page 'll be included once.

If you need some more information feel free to ask once again, we are here to help.