Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between require(__DIR__ . '/file.php') and require('file.php')

Tags:

php

As asked in the title, whats the difference between

require(__DIR__ . '/file.php')

and

require('file.php')

?

(when both files are in the same folder)

Thank you all for your help!

like image 677
Anton Stahl Avatar asked Feb 15 '16 02:02

Anton Stahl


People also ask

What is difference between require and require once in PHP?

?> The require() function is used to include a PHP file into another irrespective of whether the file is included before or not. The require_once() will first check whether a file is already included or not and if it is already included then it will not include it again.

What is difference between require_once () require () include ()?

They are all ways of including files. Require means it needs it. Require_once means it will need it but only requires it once. Include means it will include a file but it doesn't need it to continue.

What are the difference between include () and require () file in PHP?

include() Vs require() The only difference is that the include() statement generates a PHP alert but allows script execution to proceed if the file to be included cannot be found. At the same time, the require() statement generates a fatal error and terminates the script.

What is the difference between include include_once require & require_once in PHP?

The only difference between the two is that require and its sister require_once throw a fatal error if the file is not found, whereas include and include_once only show a warning and continue to load the rest of the page.


1 Answers

If you do

require(__DIR__ . '/file.php')

then you are requiring the file with the full pathname. If the file doing this require is required by another file in another directory, this require will always work. On the other hand, if you

require('file.php')

then if the file where this require statement is is required by another file in another directory, this statement will fail.

That is why it is generally good practice to include the __DIR__.

like image 97
wogsland Avatar answered Sep 24 '22 09:09

wogsland