When will a script be included twice in PHP?

I know that include_once and require_once will only allow the called script to run. My question is this: if I do not use _once , will it be executed several times? Does it run several times by default? I searched for the answers, but I did not find them.

+6
source share
4 answers

include is executed once every time you write it. The point of using include_once is what you can do

 include "myfile.php"; // Include file one time include "myfile.php"; // Include file again 

This is completely legal, and in some cases you will want to do it.

The problem is that if you, for example, define a function in this file, php complains that you cannot define this method more than once (since the name has already been executed).

include_once ensures that the same file is not included more than once, so do the following:

 include_once "myfile.php"; // Include the file one time include_once "myfile.php"; // This is ignored 

... Only one start is performed.

+7
source

It’s not that it runs several times, but if you have a file that includes several different things, and one of these ticks includes one of the other already included things that you would include them several times, which may to be bad.

+2
source

If you did not _once , it will run as many times as you call it in your script. The default number of starts does not exist (somehow I feel that you think that it can work several times on its own, but no) - one includes = one run.

+2
source

Yes, if you use include instead of include_once (or require instead of require_once ), the specified file will be included and executed a second time, if you ask for the same file a second time.

This is useful for linking to the general code that you need to execute at this point on the inclusion page, unlike the usual reason for including files that must declare functions and classes. The first is the old programming method.

0
source

Source: https://habr.com/ru/post/958750/


All Articles