This will be a programmer-ish entry. May wish to skip this one if you don’t care about PHP.
So, recently, I read someone saying that one should always use require_once to include files in PHP, rather than include, include_once, or require.
I always use include without really thinking about it, but someone claiming that require should always be used struck a wrong note with me. Upon thinking it over, I rememered that in Ye Olde Days Gone By™, require behaved exactly as named. In other words, this code would result in the inclusion of x.php:
if(false)
{
require 'x.php';
}
As distinct from this example, which would not include x.php:
if(false)
{
include 'x.php';
}
This is horribly confusing, and led to my rule of thumb that one should never use require.
This behaviour was changed in PHP 4.0.2, but the rule stuck. There is one difference though, and that is in the manner in which the error is handled when the file to be included is missing. require fails with a fatal error, but include fails with a warning, which allows execution to continue. This is kinda cool, and when you’re guaranteed to be running your script on a webserver running a recent version of PHP, I’d say either is fine - but for anything which has to support multiple versions of PHP, require becomes a dangerous choice, because it may not behave as you expect. Thus, I say that include is the wiser choice: you should notice a warning just as easily as a fatal error, and you’re guaranteed that include will behave the same way everywhere.
On to include_once and require_once: I feel that these are bad. I don’t think anyone should use them routinely. Why? Because they hide bad design. With include_once, you may never know that your script is executing twice. Or, conversely, you may want to include your script more than once, and end up with a confusing bug through force of habit. Of course, if you’re in a situation where *_once is specifically and legitimately called for, then that’s fine - but I wouldn’t use it as a matter of course. Besides, it’s case sensitive on windows servers, which sucks:
<?php
// this will include a.php
include_once("a.php");
// this will include a.php again on Windows! (PHP 4 only)
include_once("A.php");
?>
Argh!