What to use require / include / require_once / include_once?

34

I'm developing an application in PHP and would like to know when and why to use% or require or include or require_once ?

I also noticed what you can do in these ways and it works:

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

Any of the ways is correct? Are they all?

    
asked by anonymous 09.05.2014 / 10:18

3 answers

53

The difference between include and require is the way an error is handled. require produces a E_COMPILE_ERROR error, which terminates execution of script . include only produces a warning that can be "stuffy" with @ .

include_once is guaranteed that the file will not be included again if it has already been added before.

require_once is analogous to include_once

require_once is a statement therefore the syntax with parentheses may confuse a little. At first glance it may seem like a function. That's why I do not advise you to use it, although it does not have harmful consequences.

Difference of quotes

As for using single quotation marks (or apostrophe) or double quotation marks (double commas) in this case will taste. Particularly I prefer, whenever possible, to use double quotes and only use simple quotes when necessary. An example is when there are double quotation marks within string text.

In the specific case you can not have either of the double quotes in the filename or path so the pair will always be suitable. Even double quotes allow the use of variable interpolation. Eg: include "$nome.php"; (although in this case the syntax is unnecessary).

Just remembering that using the double quotes requires a double parser to handle the interpolation. It is therefore more efficient to use single quotes. But nothing that is very significant.

I would not advise using the first example shown. There is no advantage and it causes the impression that myfile.php is a symbol of the program.

As all forms are accepted by PHP all can be used, no matter the form chosen, the most important is to have standardization. Choose a shape and stick with it.

I do not know of any extra disadvantages beyond legibility in any way.

I do not know if it's obvious but you can also use require_once $minhavar; if the variable obviously contains a complete valid for a PHP file.

What can make the difference is the location where one of these statements is being used. It respects the scope. But that's another matter.

I've placed it on GitHub for future reference .

    
09.05.2014 / 11:12
10
  

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

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

    
09.05.2014 / 12:12
2
  • Error handling: include : If the file does not exist, a warning (E_WARNING) is released but your application continues to function. require : If php does not find the file, a fatal error is thrown (E_COMPILE_ERROR). in this case the script for.

    • require & include are functions of the "type" statement. 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 duplicated.

My most general recommendation is to use include_once "file"; or require_once "file"; Well, php checks to see if the file was already included / required in the script.

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

But if you do include_once from the same non-existent 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 embedded in a conditional or other function:

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

Sources: link

link

    
10.05.2014 / 19:26