The difference between include
and require
is how an error is dealt with. require
makes a mistake E_COMPILE_ERROR
, which closes the execution of script. The include
only produces a Warning which can be "muffled" with @
.
include_once
has the assurance that the file will not be included again if it has been included before.
require_once
is analogous to include_once
require_once
is a statement so parenthesis syntax can get a little confusing. At first glance it can look like it’s a function. Therefore I do not advise its use, although it does not bring harmful consequences to the program.
Quote difference
As for using single quotes (or apostrophe) or double quotes (double commas) in this case goes to taste. I particularly prefer, whenever possible, to use double quotes and only use the single one when necessary. An example is when there are double quotes inside the text of string.
In the specific case you cannot have either of the two quotes in filename or path so the double will always be suitable. Even double quotes allow the use of variable interpolation. Ex.: include "$nome.php";
(although in this case the syntax is unnecessary).
Just remember that the use of double quotes requires a double parser to treat interpolation. So it is more efficient to use single quotes. But nothing very significant.
I would not advise to use the first example shown. There is no advantage and it causes the impression that myfile.php
is a symbol of the program.
Since all forms are accepted by PHP all can be used, no matter the chosen form, the most important thing is to have standardization. Pick a way and take it.
I don’t know any extra disadvantage beyond legibility in any form.
I don’t know if it’s obvious but you can use it too require_once $minhavar;
if the variable obviously contains a path valid for a full PHP file.
What can differentiate the most is where one of these statements is being used. It respects the scope. But that’s another question.
I put in the Github for future reference.
In the field of micro-optimizations, the debate about double quotes, single quotes, concatenation of variables within strings, use of keys in strings to delimit variables, etc. is as controversial as it is controversial, with benchmarking results varying from person to person. I would only say that require/include, whether they are _Once or not language builders. I believe that statement fits better for defined purpose strings, such as a SQL query.
– Bruno Augusto
@Brunoaugusto agree with the first part. I don’t like the term "language builders" because the statements do not necessarily build something and do not know a good translation for statement.
– Maniero
Literally statement would be asserting, without any context with that side of programming, but really of the existing language builders few really contróem any. But we all know that Mr. Lerdorf is not a very wise guy. p
– Bruno Augusto
@These days I was even discussing this with some members and we didn’t like anything although "affirmation" was the obvious translation. + 1 for the last sentence :)
– Maniero