PHP parsing includes

I include an init.php file that defines path constants. Therefore, if I include init.php in a file ( index.php ) and then in another file ( layout / header.php ) .. init.php is parsed before being added to these files or added to the parent file, and then the parent file is parsed generally?

EDIT: why is this important because init.php defines path variables relative to the location where it is parsed.

+3
source share
3 answers

In fact, includethey are requireidentical in all, except that requireit will not work with E_ERROR, but includewill issue a warning. Also, both statements are activated only when they are actually executed inside the script. Thus, the following code will always work:

<?php
echo "Hello world";
if (0) require "non_existing.php";

The answer to your question is what index.phpwill be first analyzed and executed. Then, when include "init.php"it encounters, the file is init.phpparsed and executed in the current area. The same for layout/header.php- will be analyzed first.

As already noted, it init.phpwill be parsed and executed every time include/ is called require, so you probably want to use include_onceor require_once.

+4

, . require_once include_once, , .


.php .txt. php .txt, , <?php.

+3

, include require. include , . require , , .

If you want to make sure that the initialization file is loaded only once during the execution of the request, use require_onceto make sure that it is only parsed once, no matter how many times you call it. It is a good idea to try to download the init file wherever you need its constants, but use require_onceit to make sure you don't parse it more than you need.

0
source

All Articles