What is the whole argument in Exit() for?

Asked

Viewed 219 times

7

In PHP, when we want to close a script, we usually use the function exit().

When we use strings, it terminates the execution of script and print that string.

Example:

exit('Stack Overflow'); // Stack Overflow

However, if we use a value of the type int, he does not return it;

Example:

exit(0); // Nada é retornado

I imagine that this should serve for some internal functionality of this function.

But after all, what is this whole parameter in exit()?

3 answers

8


In the PHP manual says:

If status is an integer, that value will be used as the Exit status and not printed.

That is, if you passed an integer it will be used only as a status code and not printed on the page. Apparently it has been since PHP >= 4.2.0.

8

It returns this value to what called the script PHP, in this case the shell operating system. As far as I know this is of no use if you are using an HTTP server.

Obviously only the shell will know what to do with this error code that it will receive from PHP. So there is a semantic difference in this construction when using a string or a number.

Zero indicates no errors, another number indicates the reason for the failure of script.

Documentation.

  • Just so I understand. This 0 to 255 is because of the values of constants E_ALL , E_ERROR and etc?

  • 1

    As far as I know, no. This number is you who puts to indicate what you want.

3

If an integer value is passed, nothing is returned. Parameter 0 indicates that the executed code was successful. Parameter 1 is the opposite, that is, it indicates that there is an error in the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.