Including Code

Anywhere you are unconditionally including a class file, use require_once. Anywhere you are conditionally including a class file (for example, factory methods), use include_once. Either of these will ensure that class files are included only once. They share the same file list, so you don't need to worry about mixing them - a file included with require_once will not be included again by include_once.

include_once and require_once are statements, not functions. Parentheses should not surround the subject filename.
Comments (Previous) PHP Code Tags (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

Note by: StanE
I think that since SPL is part of PHP for a long time now, no includes should be used at all anymore. Namespaces have much more advantages. They automatically include files and they do this only when it is required (so memory is saved - especially if you use frameworks). They also define the folder structure (which is an advantage imho, not a disadvantage) so you don't have to care how much folders you have to go down or up and the namespaces prevent name collisions. Also you can specify the file extension(s) through the SPL at one place, instead writing them in every include statement (imagine your hoster is a cool badass and is using PHP 7 on the server but your PHP application needs to run under PHP 5 so you have to use .php5 file extensions - have a nice day changing your code...).
Note by: daevid@daevid.com
To change all require_once('foo.php'); to require_once 'foo.php' execute this:

cd /var/www/

find . -name '*.php' -print | xargs egrep -l \
'require_once\s*(\(.*\));'\ | xargs sed -i.sedorig -e \
's/require_once\s*(\(.*\));/require_once \1;/'

(thanks to Robert Hajime Lanning for that)

Then to remove all the ".php.sedorig" backup files execute this:

find . -name "*.php.sedorig" -type f -exec rm -rf {} \;