Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between require, include, require_once and include_once?

In PHP:

  • When should I use require vs. include?
  • When should I use require_once vs. include_once?
like image 692
Scott B Avatar asked Mar 10 '10 16:03

Scott B


People also ask

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

They are all ways of including files. Require means it needs it. Require_once means it will need it but only requires it once. Include means it will include a file but it doesn't need it to continue.

What is the difference between include and include_once statements?

The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns true .

What is the difference between require and include '?

Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found.

What is include Include_once?

The include_once keyword is used to embed PHP code from another file. If the file is not found, a warning is shown and the program continues to run. If the file was already included previously, this statement will not include it again.


1 Answers

There are require and include_once as well.

So your question should be...

  1. When should I use require vs. include?
  2. When should I use require_once vs. require

The answer to 1 is described here.

The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

The answer to 2 can be found here.

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

like image 114
Leo Avatar answered Oct 12 '22 08:10

Leo