View Full Version : Using require vs require_once() - i am a bit confused !

08-30-2006, 11:25 AM
Hello all,

I have been using require_once() throughout my scripts because I thought it would save the processor time keep looking up the same function include file.

But it seems I might be wrong !

The Manual says

The require_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the require() statement, with the only difference being

Does this mean the require_once() is only useful if you use it in the same script more than once ?

I have about 30 different scripts that start like this:

* a_get_detail.php
* Connects to tables and extracts the data


other code ....

I have also just read that require_once() is slower than the standard require() function.

In this case is it better for me to be using require() as I don't think I am gaining anything from using require_once(). :confused:

Thanks for any advice. :)

08-30-2006, 12:00 PM
require_once is the "safer" function as it will not allow you to include a file more than once which can couse errors. You shuoldn't use it when you do want to include a file more than once.

08-30-2006, 12:16 PM
Well in this case the file just contains my
data cleaning functions eg

function safe_sql_br( $value )
$value = nl2br($value);
$value = trim(strip_tags($value,'<br>'));
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
// Quote if not integer
if (!is_numeric($value)) {
$value = mysql_real_escape_string($value);
return $value;
} // End of Function

So in this case would you say its best to use

or require()


08-30-2006, 01:43 PM
By those errors I ment that the programmer may not fully know what he is doing and get errors he wouldn't expect (for example a "cannot redeclare function" error). If you know what you are doing you can use require().
require_once can still be useful for cases where you make sure a file is included (and not incuded if it already was).

08-30-2006, 04:51 PM
actually, if you read carefully, from fastest to slowest:


Include just brings the file in.

Require first stats the file and then brings the file in

Include_once has to go through the entire loaded files table to ensure the file hasn't been included, then it brings it in.

require_once first stats the file, then looks through the entire loaded files table to ensure the file hasn't been included, then brings it in.

08-30-2006, 06:41 PM
include() would also stat the file. It returns a warning if the file does not exist.

08-31-2006, 09:43 PM
You're right, I was mis-remembering this from the PHP documentation.

Note: Prior to PHP 4.0.2, the following applies: require() will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.