When is a php include file parsed?

纵然是瞬间 提交于 2020-01-11 11:29:31

问题


When is a PHP include file parsed? At startup, or during execution?

My web forms call a single php script. Depending on the arguements passed in the URL, a switch/case condition determines what the script will do. Each "case" within the switch has its own include files.

If include files are parsed during initial load, then my php script will take up more memory/time to process which leads me to believe having individual php files called from my web form is better, than having one which includes what it needs.

If include files are parsed when needed (thus, when a branch of the code reaches a specific case statement, that it then performs the include) it tells me my code will be reasonably conservative on memory.

So.... my question... When is a PHP include file parsed? At initial load, or during execution?

(note... I failed to find the answer here, and I have read http://php.net/manual/en/function.include.php)


回答1:


Files are included if and when the include statement is reached at runtime. To very succinctly summarise what that means, the following file is never going to be included:

if (false) {
    include 'foo.php';
}



回答2:


Since you're concerned about memory usage from too many includes, I feel that a bit more detail will be useful over and above a direct answer to your question.

Firstly, to directly answer you, PHP files are parsed as soon as they are loaded -- if a file contains a syntax error, you will be told of that immediately; it won't wait till it gets to that line of code. However subsequent files are only included if the specific line of code containing the include statement is executed.

You're concerned about memory usage, but having a lot of included files is generally not a major memory issue, nor a major performance issue. Indeed, most modern PHP applications of any size will use a framework library that load hundreds of PHP files for every page load. Memory and performance issues are far more likely to be caused by bugs within your code rather than simply loading too much code.

If you are concerned about memory and performance from this, you should consider using PHP's OpCache feature. With this feature enabled, PHP stores a cache in memory of the compiled state of all the files it has included within a system. When it runs the page again, therefore, it does not need to actually load or parse anything when it encounters an include statement; it simply fetches it from the cache.

Using OpCache you can write your code with a very large number of files to include, and without any performance penalty at all.

The good news is that OpCache is enabled by default in recent PHP versions, and is completely transparent to the developer -- you don't even need to know that it's there; the only difference you'll see between it being turned on and off is your site running faster.

So, firstly, make sure your PHP version is up-to-date (v5.5 or higher). Then make sure OpCache is enabled in your PHP.ini file. Then just sit back and stop worrying about these kinds of things.




回答3:


File included with include statement are parsed during exection. When your php code hits a include statement it will start parsing the file to see what is in there. From w3schools

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.

There is other questions with a similar topic:

In PHP, how does include() exactly work?



来源:https://stackoverflow.com/questions/37880749/when-is-a-php-include-file-parsed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!