Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Integrating with PayPal Redirecting Invalid Shirt IDs

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

Exit Function

Why should i be using the exit() function at the end of the ifs? What happens if I dont? Why is it a good habbit?

Thanks,

Giovanni

3 Answers

Why is it a good idea? Imagine if you don't and these conditionals are processing cc numbers. You'd have a lot of explaining to do.

The exit function is simply to halt any other PHP from being processed. So by using this in the conditional when the conditional fails, you're safe guarding your visitors from unintended content, processing of items that shouldn't have executed and more.

Granted there's no hard rules on code, just guidelines that work. I hope this helps.

Hello,

Michael explains it great. Also, it's a good idea to put exit() or die() after redirects as well so no other code gets ran or the rest of the page doesn't display in the amount of time it takes for the redirect to take place. exit() and die() are the same thing and there is no better one over the other it's just preference,

I would like to include with Michael's statement that sometimes in your coding you want to have dead ends where you want the page to finish early for whatever reason. PHP runs your script from top to bottom and doesn't stop till either it reaches the end of the script, it errors out (run-time errors), or if it gets redirected off the page. You would use the exit() or die() to tell the parser to stop processing the script. Learn more about exit() here and die() here.

Cheers!

Giovanni Valdenegro
Giovanni Valdenegro
13,871 Points

Thank you Shawn great explanation. I understand now why it is wise to use die or exit.

I appreciate you answering this question.

Giovanni