What to use require/include/require_once/include_once?

Asked

Viewed 57,228 times

41

I am developing an application in PHP and would like to know when and why to use require or include or require_once or include_once?

I also noticed that you can make these shapes and it works:

require_once myfile.php;   
require_once 'myfile.php'; 
require_once ('myfile.php');
require_once ("myfile.php");
require_once "myfile.php";

Either way is correct? They’re all?

3 answers

64


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.

  • 1

    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.

  • @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.

  • 2

    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

  • @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 :)

11

require is identical to the include, except in case of failure that will also produce a fatal level error E_COMPILE_ERROR. In other words, it will stop the script while the include only issue a warning (E_WARNING) that allows the script to continue.

The addition of _once (for both cases) tells PHP to check if the file has already been included, if it has been will not be included again.

4

  • Error handling: include: if the file does not exist, a Warning (E_WARNING) is launched but your application keeps working. require: if php does not locate the file, a fatal error is released (E_COMPILE_ERROR). in this case the script for.

    • require & include are "type" statement functions. The correct semantics of using these functions is: include 'file.php' || require 'file.php';

If you use these functions repeatedly on the same page (unnecessarily or by accident), it will be included or required duplicately.

My most general recommendation is to use include_once "file"; or require_once "file"; Because php checks whether the file has already been included/required in the script.

  • OBS: function include_once, when it does not find the file informed, its return is a value boleano false:

but if by chance you do include_once of the same nonexistent file, it returns true!

<?php var_dump(include_once "fake_file.php"); #retorno true ?>

Another point: the use of parentheses instead of "": i use parentheses when my include is inserted in a conditional or other function:

<?php if(!@include_once("fake.php")){echo "fake.php não incluso";}

Sources: http://www.w3schools.com/php/php_includes.asp

http://www.php.net/manual/en/function.include.php

  • I don’t think there’s any correct semantics since they’re all correct.

  • When I say correct semantics, I say follow the letter of the php documentation, but in fact, they are all really correct :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.