r/PHPhelp • u/Nice_Magician3014 • Dec 03 '24
Does PHP complier read all the code at once
For example, if I have the following code:
if(1==2){include "really_big_file.php";}else{include "small_file.php";}
will the compiler read and parse all the code in "really_big_file.php" anyway? I know it will not execute it, but will it read it from disk and load it in memory?
18
Upvotes
24
u/HolyGonzo Dec 03 '24
Instead of giving you a quick answer, just put an intentional syntax error inside really_big_file.php and run your test.
Going through the motions is a good way to build your knowledge.
13
u/colshrapnel Dec 03 '24
Come on guys, if you so much upvote the answer, upvote the question, which led to that answer, as well. Not to mention this is r/PHPHelp which is meant for questions like this, so it doesn't deserve downvotes either.
2
u/Aggressive_Ad_5454 Dec 03 '24
It definitely doesn’t perform the include operations until it executes the lines containing them.
This comes in handy if you have a big chunk of initialization code, or something like that, that you don’t need to run as often as other code. Speeds things up a bit.
Suggestion: read up on require_once for another potential optimization. include_once is also a thing.
Php has a so-called op code cache, a global data structure that holds the parsed results of source code. So none of these optimizations are as effective as they seem, especially in a busy web server environment.
0
u/colshrapnel Dec 03 '24
FYI, nobody can know what you mean. Besides, this is a public resource and other people could read your question and get wrong idea on PHP's nature. Correcting the term is the right thing to do. No need to be that touchy.
1
u/nielsd0 Dec 03 '24
No
-1
u/[deleted] Dec 03 '24
PHP does not compile. It's on demand.
Use xdebug, place a BP before condition. And run step by step. Hé will includes only one file.
Other way to test, make on include good, another to a non existing file.
Thé error will only popup if condition meet the second case.
0
u/[deleted] Dec 03 '24
[deleted] — view deleted comment