This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Requiring Files


Require is very common to include, with one major difference:
If you try to include a file that does not exist you will get an error, but your page will continue to load.
If you try to require a file that does not exist you will get an error, and you page will stop loading everything after that.
To use require is the exact same as using include:

<? require("MyFile.php"); ?>

Here are a few examples of how include and require act when they can't find a file. This is how include handles a missing file:

<?

include("thisFileDoesNotExist.php");
echo "My text still shows up!";

?>

Displays:

Warning: include(thisFileDoesNotExist.php) [function.include]: failed to open stream: No such file or directory in /home/directory/public_html/folder/index.php on line 2

Warning: include() [function.include]: Failed opening 'thisFileDoesNotExist.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/directory/public_html/folder/index.php on line 2
Hi, my name is Kalob

It still showed the string we tried to echo!
Now let's look at how require handles this:

<?

require("thisFileDoesNotExist.php");
echo "My text will not show up";

?>

Displays:

Warning: require(thisFileDoesNotExist.php) [function.require]: failed to open stream: No such file or directory in /home/directory/public_html/folder/index.php on line 2

Fatal error: require() [function.require]: Failed opening required 'thisFileDoesNotExist.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/directory/public_html/folder/index.php on line 2

Our string we wanted to echo did not show up..

So why should we use require if it will stop our page if it errors?
... I don't really have an answer for that. Every coder is a little different, and prefers different methods. I personally use require, and very rarely will I ever use include. I do this for two reasons: I know for a fact that the file I specify exists, or if I mistype file name and I test my page it will tell me exactly where the problem is, since it's the last thing that will show up in on your page.
While your learning PHP, I'd recommend using include, purely because it's a little more simple.

Task Discussion


  • Asier Iturralde Sarasola   April 14, 2012, 9:28 p.m.

    There are include_once and require_once as well.

    I found an interesting comment made by the Stackoverflow user Gordon on when to use each one here:

    Use

    when the file is required by your application, e.g. an important message template or a file containing configuration variables which with without the app would break.

    when the file contains content that would produce an error on subsequent inclusion, e.g. function important() { /* important code */} is definitely needed in your application but since functions cannot be redeclared should not be included again.

    when the file is not required and application flow should continue when not found, e.g. great for templates referencing variables from the current scope or something

    optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead


    But basically, it's up to you, when to use which.
     

    I copied the comment here thanks to the cc-wiki (aka cc-by-sa) license of Stackoverflow.