What is Exit (integer) and how to use it in PHP

I don't know anything about C, C ++, or any lower level than PHP. I look into the Codeigniter 3 codes on github and find that it has added exit status code constants , i.e. we can do:

exit(EXIT_DATABASE) means exit(8) or exit(EXIT_UNKNOWN_CLASS) means exit(5)

what is different between

 echo 'Configuration file not found'; exit(3); 

and just

exit('Configuration file not found'); ?

What is the purpose of using exit(integer) in php? he doesn't print anything, does he? I also check documents and some others, but still do not understand. How to use this? Where can I get a link about this?

Thanks.

+7
source share
4 answers

It gives a hint a hint about what the result of running your script is.

This can be useful in php if you use a script with exec or system , and you need to behave differently depending on the result of running the script.

 <?php $output = array(); $error = null; exec("/path/to/php cleanData.php", $output, $error); if ($error){ Logger::log($error, $output); die("Sorry I was Unable to Clean the Data\n"); } 
+3
source

If you use some of your scripts from the console, you can determine any error from the script response code.

+1
source

http://php.net/manual/de/function.exit.php

You can use an integer to return error codes that may be used by former programs. For example, you can use NAGIOS to monitor your servers, in which case PHP-Script is called, for example. to make a DB call, to count something, anything. At the end of the script, you return 0,1,2,3 as a return code to tell NAGIOS if the check you are doing is approved, warns, is critical, or is unknown. This return code is then used by NAGIOS for further actions, such as sending email to the administrator, etc.

So you can use exitcode to provide information to other programs that use your PHP script

+1
source

I think the guide is very obvious that you can look at this example

 //exit program normally exit; exit(); exit(0); //exit with an error code exit(1); exit(0376); //octal 

It is used to exit the program from the console either with an error or without it, so you can track them, and it is exaclty similar to the die() function.

If the status is an integer, this value will be used as the exit status, and not for printing. Exit statuses must be in the range of 0 to 254, exit status 255 is reserved by PHP and should not be used. State 0 is used to complete the program successfully.

As you said, the function also allows you to directly print your error if you use it with string instead of an integer

If the status is a string, this function prints the status before exiting.

Link

0
source

All Articles