Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

When we use ( die and exit ) on PHP ?

user-image
Question added by Adel Ezat Fawzy Ellozy , Webdeveloper. , Saudi Arabian Maritiem Sports Federation
Date Posted: 2017/03/06
Abdul Manan
by Abdul Manan , Team Leader , iThink Innovation Solution

die() and exit() are precisely identical; they halt the entire PHP program and return to the OS. They're two different names for the same function.

return, on the other hand, ends a function call and returns to the caller. At the end of a program, return sets the status value that is returned to the OS; the program is going to exit no matter what.

Moeed Nisar
by Moeed Nisar , Senior Web Designer & Developer , Intelligent Solutions Pvt. Ltd

In PHP, die & exit are the two different names of the same function, both are language construct and equivalent to each other, 

 

although in using both the functions, i have a personal practice.

 

EXIT()

when i want to end the execution of the script, i use exit.

 

DIE()

when i need to end the execution due to some problem (e.g. could not connect to db) i use die()

 

This way, when I see exit at some point in my code, I know that at this point I want to exit because the logic ends here. When I see die, I know that I'd like to continue execution, but I can't or shouldn't due to error in previous execution.

alaa liswe
by alaa liswe , ِAdministrative Assistant , Arab Open University

– There is not a single visible difference between these two functions. They both are the same, one is the alias of the other. If at all any one of you can find a real visible difference then I would appreciate if you could post it in the comments section of this blog.

die() closes the connection and exit() doesn't. Check the broweser header information in connection You will find for example connection:keep-alive while using exit and connection:close while using die.

Kamal Hasija
by Kamal Hasija , Web Developer , The Brihaspati Infotech Pvt. Ltd.

Hi @Adel , thanks for the invitation Sir:-

 

Both are same because these are construct. They halt the entire PHP program and returns.

According to Die it is Equivalent to Exit. So yes, you can interchange them. Maybe the die function call was created to make Perl programmers feel at home. aliases allows programmers to use the one which is comfortable with. I remember exit better than die. Some others remember die better than exit.

 

Regards,

 

Kamal

More Questions Like This