Tell me about the die and exit of PHP.

Source: Internet
Author: User
Tags php source code
Today, a small partner said that exit and die a little different. I say die is not the alias of exit? In order to prove my point of view, turned over PHP source code, in the ZEND_LANGUAGE_SCANNER.L, it is easy to find that the keyword is the same token:

  "Exit" {    return t_exit;}
   "Die" {    return t_exit;}

So the end is the same opcode:zend_exit. So there's no difference between these two keywords, and there's really nothing to say.

I reminded my little friends: do not use exit to output integers. The reason is also very simple, in the PHP official website document can see:

void exit ([string $status])

void exit (int $status)

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

If status is an integer, the value will be used as the exit status code and will not be printed out. The exit status code should be in the range 0 to 254 and should not use the exit status Code 255 reserved by PHP. Status code 0 is used to successfully abort the program.

So if the status is an integer, it will be treated as a status code output instead of printing, so it is not possible to return to the front end.

So what's the use of this status code?

As you know, shell script execution can return a status code, and the execution of the PHP script returns the same status code, which can be captured in the environment variable:

Scholer: ~ $ Php-r ' exit (254); ' Scholer: ~ $ echo $?254

My curiosity was hooked up: What if I gave a status code that wasn't between 0 and 255? After testing, it is found that if the status code is greater than 255, it will return the result of status to 256. If it is less than 0, the result of the sum of status 256 is returned between 1 ~ 255, and the value of less than 256 is absolute and 256 is the remainder. In short, between 0 ~ 255.

Then go on to explore.

Exit is implemented in Zend_vm_def.h:

Zend_vm_handler (Zend_exit, const| tmp| var| unused| CV, any) {#if!defined (zend_vm_spec) | | (Op1_type! = is_unused)    Use_opline    save_opline ();    if (op1_type! = is_unused) {        zend_free_op free_op1;        Zval *ptr = get_op1_zval_ptr (bp_var_r);        if (z_type_p (ptr) = = Is_long) {            EG (exit_status) = Z_lval_p (PTR);        } else {            zend_print_variable (PTR);        }        Free_op1 ();    } #endif

From the code we can clearly see through the z_type_p to detect the type of status code, if it is long, then assign to the global variable Exit_status (EG This macro is used to easily access global variables), if not, call Zend_print_ Variable print out.

Z_lval_p's declaration is in zend_operators.h:

#define Z_LVAL_P (zval_p)        z_lval (*zval_p) ... #define Z_LVAL (zval) (Zval            ). Value.lval

Further, we all know that the variables in the PHP interpreter are defined (I this source or PHP 5.5 version, not PHP7), in the zend.h:

typedef Union _ZVALUE_VALUE {    long lval;                    /* Long value */    double dval;                /* Double value */    struct {        char *val;        int len;    } STR;    HashTable *ht;                /* Hash Table value */    zend_object_value obj;} zvalue_value;struct _zval_struct {/    * Variable Information */
  zvalue_value value;        /* Value */    zend_uint refcount__gc;    Zend_uchar type;    /* Active type */    Zend_uchar is_ref__gc;};

So here the value of Exit_status is still a long plastic.

So the question is, why is the final output a status code between 0 and 255? Honestly this problem I eat is not very good, this needs to be familiar with the Linux environment programming enough to do, here can only be said briefly.

Follow the execution through Strace:

$ strace php-r ' exit (258); ' >& Strace.log

The last two lines of the results can be clearly seen:

... exit_group (258)                         = + + + exited with 2 + + + +

Exit_group is still the original value, but will eventually become 2. PHP itself does not specifically handle this value, but exit or return in the main function can only use values between 0 and 255, and other values will be processed. You can write a simple program test:

int main (int argc, char const *argv[]) {    return 258;}


Scholer: ~ $./testscholer: ~ $ echo $?2

For details, see:

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.