This is basically an idea I have for PHP to support a main entry point.
Where regardless of execution, if you run a PHP file, in CLI or Web request.
If the file that is executed contains a main function, that is automatically executed by the engine after any procedural code.
Any procedural code in the file, and included files are executed before main(). I made this decision because there would be cases where developers want to set up a global scope and variables.
The main entry function MUST be within the executed file, any included files which contain a main function are treated as user functions.
main function signature: function main(): int;
main must return an int exit code.
Example
echo "Before main";
function main(): int
{
echo "Executed in main";
return 0;
}
echo "After main";
return 1; // This is ignored as main() exists and its return value is the exit code.
Expected output:
Before main
After main
Executed in main
PHP returns code 0
Open questions.
1. Should we add a declare(main_entry_point=true); for it to be opt-in
2. Should main() take arguments?
3. Should the main() be context aware? eg main(array $argv, int $argc)
for the CLI SAPI. I’m not sure what it would be for CGI SAPI.
On Fri, Jul 4, 2025, 16:50 fennic log <fenniclog@gmail.com> wrote:
This is basically an idea I have for PHP to support a main entry point.
Where regardless of execution, if you run a PHP file, in CLI or Web request.
If the file that is executed contains a main function, that is automatically executed by the engine after any procedural code.
Any procedural code in the file, and included files are executed before main(). I made this decision because there would be cases where developers want to set up a global scope and variables.
The main entry function MUST be within the executed file, any included files which contain a main function are treated as user functions.
main function signature: function main(): int;
main must return an int exit code.
Example
echo "Before main";
function main(): int
{
echo "Executed in main";
return 0;
}
echo "After main";
return 1; // This is ignored as main() exists and its return value is the exit code.
Expected output:
Before main
After main
Executed in main
PHP returns code 0
Open questions.
1. Should we add a declare(main_entry_point=true); for it to be opt-in
2. Should main() take arguments?
3. Should the main() be context aware? eg main(array $argv, int $argc)
for the CLI SAPI. I’m not sure what it would be for CGI SAPI.
Why though?
You are just saving seven keystrokes.
main();
Besides, PHP already has main function so what you are proposing would be confusing.
fennic, you don’t get the concept of the main
function. Nothing will be printed before nor after. Only what is inside it as a rule nothing else is executed outside that scope. You will end up with “headers already sent” and so on with that proposition.
Kamil, you forgot to count the enter button for the new line… sheesh 
···
Iliya Miroslavov Iliev
i.miroslavov@gmail.com
I made the decision to keep precedule code to execute first due to, for example, where you need to include(./vendor/autoload.php)
If we limit execution to only the inside main() function where main() exists, everything would break, this the keeps BC with existing codebases.
···
Iliya Miroslavov Iliev
i.miroslavov@gmail.com
On 4 July 2025 15:49:59 BST, fennic log <fenniclog@gmail.com> wrote:
This is basically an idea I have for PHP to support a main entry point.
Where regardless of execution, if you run a PHP file, in CLI or Web
request.
If the file that is executed contains a main function, that is
automatically executed by the engine after any procedural code.
Can you explain what problem or use case this is trying to solve?
As others have pointed out, if you're running procedural code and then calling "main" with no arguments, it's equivalent to adding one line of code in the file, even including the return code:
exit( main() );
Is there a specific scenario where you see an automatic call being a significant improvement over that line?
Rowan Tommins
[IMSoP]