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.