Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trigger_error will terminate script #3694

Open
theking2 opened this issue Aug 28, 2024 · 4 comments
Open

trigger_error will terminate script #3694

theking2 opened this issue Aug 28, 2024 · 4 comments

Comments

@theking2
Copy link

in https://www.php.net/function.trigger-error there is no mention of an exit(1) being issued when $error_level is E_USER_ERROR by the default error handler.
Also it would be helpful that it will not if a registerd error handler does not issue and exit(1)

@theking2 theking2 changed the title trigger_error will terminate scrip trigger_error will terminate script Aug 28, 2024
@KERNAMON
Copy link

in https://www.php.net/function.trigger-error there is no mention of an exit(1) being issued when $error_level is E_USER_ERROR by the default error handler. Also it would be helpful that it will not if a registerd error handler does not issue and exit(1)

The documentation indeed does not explicitly state that the default error handler issues an exit(1) when $error_level is E_USER_ERROR. However, the behavior of the default PHP error handler for E_USER_ERROR includes terminating script execution. This is equivalent to calling exit(1) internally, as the process exits with a status code indicating an error.

When E_USER_ERROR is triggered, the default error handler stops script execution. This is PHP's standard behavior for fatal errors, including user-defined fatal errors (E_USER_ERROR).

The phrase "Passing E_USER_ERROR as the error_level is now deprecated. Throw an Exception or call exit() instead." does not explicitly mandate that exit(1) must be used. It allows for any exit status to be used. However, the convention in most programming practices is to use exit(1) or any non-zero status to indicate an error, while exit(0) indicates successful termination.

The absence of exit(1) in the default handler documentation doesn't negate its behavior of script termination. When using custom handlers, this termination behavior must be manually handled. The statement in the documentation gives you the flexibility to choose the appropriate exit status code based on your application's needs.

We have noticed that this matter is being discussed since 2011, on StackOverFlow.

CONCLUSION:

No, exit(1) is not automatically issued when $error_level is E_USER_ERROR if a registered error handler does not explicitly terminate the script.

If you register a custom error handler using set_error_handler(), the default behavior of PHP (which terminates the script for E_USER_ERROR) is overridden. In this case, the custom error handler has full control over what happens, and PHP will not automatically terminate the script unless you explicitly call exit() or throw an uncaught exception in your error handler.

Hope it helps.

@kamil-tekiela
Copy link
Member

Unless I am misunderstanding the issue, the E_USER_ERROR doesn't trigger exit(1) or any other exit. The default error handler will stop the script, but that's just how the default handler operates. As such this information would be more suitable on https://www.php.net/manual/en/function.set-error-handler.php. Perhaps it would be a good idea to document how the default error handler operates in more detail.

@theking2
Copy link
Author

https://www.w3docs.com/learn-php/trigger-error.html

trigger_error with E_USER_ERROR.

The E_USER_ERROR type is used for fatal errors that should stop the script from executing.

@theking2
Copy link
Author

Unless I am misunderstanding the issue, the E_USER_ERROR doesn't trigger exit(1) or any other exit. The default error handler will stop the script, but that's just how the default handler operates. As such this information would be more suitable on https://www.php.net/manual/en/function.set-error-handler.php. Perhaps it would be a good idea to document how the default error handler operates in more detail.

I indeed think it would

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants