0

I think this code:

if(file_exists("amodule.inc.php")) 
    require_once("amodule.inc.php");  

is misleading because of the use of the require_once.

I think that - to keep the logic and "wording" in line - "include_once" would be appropriate (where there are a number of arguments to not use the "if" at all but I would like to concentrate on "require vs include").

As far as my understanding goes, the ONLY difference between "require" and "include" is that "require has a consequence (halt) if the file does not exist vs. include proceeds with just a warning.

But, in the example, if the file does not exists, the require_once code will not executed anyways. Therefore the "require" misleads from my point of view.

From a superficial view, one could argue that the above code using require_once and

if(file_exists("amodule.inc.php")) 
    include_once("amodule.inc.php");

are "identical" what, from my view, is not. Because:

A "rough" analysis (like an automated check of a project) would throw a message, that "amodule.inc.php" is a vital project file, which the code shows, is not.

Also, in a (not very likely) cas that between the execution of the "if" statement and the require_once statement, the file could be deleted. Then, even worse, the code would NOT execute like expected (to run without load) but give up.

So, how would you guys out there argue?

3
  • 1
    why not @include_once? Commented Oct 8, 2013 at 5:33
  • 2
    I dont think I'd 'argue' over something this trivial. This really belongs on your blog. Adding 'what do you think?' at the end does not make a blog post a valid question. Commented Oct 8, 2013 at 6:40
  • @WinstonEwert: Because eating every possible error state in order to poorly ignore one particular failure is terrible practice. The @ operator is essentially language-level support for try {...} catch(Exception){}. Avoid.
    – Phoshi
    Commented Oct 8, 2013 at 10:51

2 Answers 2

3

You are right about the first difference. Include and require are the same, except require will stop your program from running, if the file is missing, whereas include will continue to run and show an error.

From the PHP Manual:

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.

So if you have a large number of arguments, then, yes the include_once would be preferable, but without seeing the code, it would depend on what you want to happen if the file is missing.

8
  • You seem to have quoted the differences between xxx_once and xxx but omitted those between require and include. Commented Oct 6, 2013 at 15:13
  • 1
    No. They all there read again
    – user208372
    Commented Oct 6, 2013 at 22:15
  • "The require_once statement is identical to require," "The include_once statement...similar to the include statement," and "avoid problems such as function redefinitions." All of these relate to the "once" aspect of require_once and include_once. None of the quoted material addresses the difference between require and include. I'm not saying that your answer is incorrect, only that the parts you quoted are irrelevant. Commented Oct 7, 2013 at 1:53
  • @DavidHarkness "Include and require are the same, except require will stop your program from running, if the file is missing, whereas include will continue to run and show an error." ..//... "So if you have a large number of arguments, then, yes the include_once would be preferable, but without seeing the code, it would depend on what you want to happen if the file is missing." <-- David I am not sure why you don't see those statements as an answer between the differences.
    – user208372
    Commented Oct 7, 2013 at 3:05
  • 2
    @ThinksALot - I'm talking about the quotes from the PHP manual. They are unrelated to your answer. The part you wrote yourself is fine; it just looks weird to quote unrelated lines from the manual as reference. See my edit. Commented Oct 7, 2013 at 18:25
1

The fact that a file exists does not mean it is readable, that's a first issue:

http://php.net/manual/en/function.is-readable.php

With includes some other strange things happen, see for example:

http://php.net/file_exists

Warning This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

So the check of file_exists is the most well-known, but maybe not the most right method, to see if you can include a file.

This seems to be the right solution:

http://php.net/stream_resolve_include_path

Resolve filename against the include path according to the same rules as fopen()/ include.

That really checks whether you can include the file.

Then: Should you require it: Yes in most cases. If a class file exists and is includable than the loading should succeed. If php files cannot be loaded correct there is an, in my opinion, big issue with server / software which we would call fatal. So, for us it is valid to use require_once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.