What to use require / include/require once / include once?

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 do 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";

Is any of the forms correct? Are they all there?

Author: Zuul, 2014-05-09

3 answers

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

include_once you are guaranteed that the file will not be included again if it has been included before.

require_once it is analogous to include_once

require_once it is a statement so syntax with parentheses can be a bit confusing. In the first look it may seem that it is a function. So I do not advise its use, although it does not bring harmful consequences to the program.

Quotation mark 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 it is necessary. An example is when there are double quotes within the text of the string.

In the specific case you do not it can have neither quotation mark 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 remembering that using double quotes forces a double parser to handle interpolation. Therefore it is more efficient to use simple quotes. But nothing that is very significant.

I would not advise using the first example shown. There is no advantage and it gives 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. Choose a shape and stick with it.

I am unaware of any extra disadvantages other than readability in any form.

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

What can differentiate the most is where one of these statements is being used. He respects the scope. But this is already another matter.

I put on GitHub for future reference.

 63
Author: Maniero, 2020-09-02 14:20:03

require it is identical to include, except in case of failure which will also produce a fatal error of Level E_COMPILE_ERROR. In other words, it will stop the script while include only issues 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 it will not be included again.

 10
Author: Dalton Menezes, 2014-05-09 10:12:06
  • Error Handling: include : if the file does not exist, a warning (E_WARNING) is thrown but your application continues to work. require : if php does not locate the file, a fatal error is thrown (E_COMPILE_ERROR). in this case the script stops.

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

If you using these functions repeatedly on the same page (unnecessarily or by accident), the same will be included or required in duplicate.

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

  • OBS: function include_once, when it does not find the informed file, its return is a bolean value 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 into 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/pt_BR/function.include.php

 3
Author: Samuel Diogo, 2014-05-10 17:26:48