# [RFC Idea] main() entry point for PHP

- Thread: https://externals.io/message/127877
- Original archive: https://news-web.php.net/php.internals/127877
- Started by fennic log on 2025-07-04T14:49:59+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #127877 — fennic log — 2025-07-04T14:49:59+00:00

URL: https://externals.io/message/127877#127877

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
```php
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.

---

## #127878 — Kamil Tekiela — 2025-07-04T15:06:06+00:00

URL: https://externals.io/message/127877#127878 · In reply to: #127877

> 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
> ```php
> 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.

---

## #127882 — Iliya Miroslavov Iliev — 2025-07-04T21:03:11+00:00

URL: https://externals.io/message/127877#127882 · In reply to: #127878

>
>
> 
>
> > […]
>
> 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

---

## #127904 — fennic log — 2025-07-05T14:01:16+00:00

URL: https://externals.io/message/127877#127904 · In reply to: #127882

On Fri, 4 Jul 2025 at 22:03, Iliya Miroslavov Iliev <i.miroslavov@gmail.com>
wrote:

>
>
> 
>
> > […]
>
>
> 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.

---

## #127914 — Rowan Tommins [IMSoP] — 2025-07-06T11:12:40+00:00

URL: https://externals.io/message/127877#127914 · In reply to: #127877

>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]
