mds1256 Posted February 5, 2018 Share Posted February 5, 2018 Hi If I have two php files, and one calls a function in another, if I want to return a response and then call exit; to make sure nothing else runs (part of error handling is to send a response and then force exit), do I call exit in the function or in the script after calling the function? Or does it really not make any difference? e.g. <?php include_once('otherfile.php'); functionName(); // call exit; here? ?> otherfile.php <?php public function functionName() { echo "Error: function error"; // or if I call exit; here? } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 5, 2018 Share Posted February 5, 2018 You would call return to return from a function and call exit from the main line code that has called that function. With a return you can send back any kind of result that you wish so that the caller is informed properly. Exit is simply a departure from PHP. Quote Link to comment Share on other sites More sharing options...
mds1256 Posted February 5, 2018 Author Share Posted February 5, 2018 (edited) You would call return to return from a function and call exit from the main line code that has called that function. With a return you can send back any kind of result that you wish so that the caller is informed properly. Exit is simply a departure from PHP. So to give you a bit more explanation, the functionName function is like an error handler, so this will be called multiple times from the main file, so I was wanting to put the exit; call inside of the functionName function so I didn't have to keep calling exit after I have called functionName all the time. e.g. if() { } else { functionName(); exit; } Edited February 5, 2018 by mds1256 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 5, 2018 Share Posted February 5, 2018 It is your decision but proper programming practice would dictate that if a mainline piece of code is calling a function then that caller will expect a return. To call a function that never returns is a maintenance-programmer's worst nightmare. If you have need to write a function to do something out-of-line then you should handle the exit from that point, not in another block of code. Really? You want to write poor code to save typing 7 chars on your keyboard? Quote Link to comment Share on other sites More sharing options...
mds1256 Posted February 5, 2018 Author Share Posted February 5, 2018 It is your decision but proper programming practice would dictate that if a mainline piece of code is calling a function then that caller will expect a return. To call a function that never returns is a maintenance-programmer's worst nightmare. If you have need to write a function to do something out-of-line then you should handle the exit from that point, not in another block of code. Really? You want to write poor code to save typing 7 chars on your keyboard? It will be called multiple times within the main document, so it was just to tidy it up (call it in the functionName once rather than multiple times). Not wanting to write poor code, hence this post, just wanted to know the proper way or doing this and if that means that I have to call exit after each function call then that is fine. Was just trying to make the code as readable as possible. Quote Link to comment Share on other sites More sharing options...
Solution ginerjm Posted February 5, 2018 Solution Share Posted February 5, 2018 Placing the exit() call following the function DOES make it as "readable as possible". Imagine you are looking at someone else's code where you see a call to a function and nothing after it and you wonder where the code is supposed to go next.... Quote Link to comment Share on other sites More sharing options...
mds1256 Posted February 5, 2018 Author Share Posted February 5, 2018 (edited) Placing the exit() call following the function DOES make it as "readable as possible". Imagine you are looking at someone else's code where you see a call to a function and nothing after it and you wonder where the code is supposed to go next....To be fair when you say it like that you’re right. Thanks for this Edited February 5, 2018 by mds1256 Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 5, 2018 Share Posted February 5, 2018 (edited) Also, I would suggest reconfiguring the logic for the function and the code that calls the function. This would be easieer to write/maintain public function checkName() { //Simply return the test condition (will return Boolean TRUE/FALE return (foo == bar); } Then in the code that call the function, configure it like this: if(!checkName) { //Ouput error condition (and exit?) } //Continue success condition - no need for an else However, I would also state that exit() is a poor way to handle error conditions. It almost always results in improperly formatted HTML pages. It doesn't take much to create an alternative path to produce a valid page with appropriate error conditions. Edited February 6, 2018 by requinix fixing bbcode Quote Link to comment Share on other sites More sharing options...
mds1256 Posted February 5, 2018 Author Share Posted February 5, 2018 Also, I would suggest reconfiguring the logic for the function and the code that calls the function. This would be easieer to write/maintain public function checkName() { //Simply return the test condition (will return Boolean TRUE/FALE return (foo == bar); }Then in the code that call the function, configure it like this: if(!checkName) { //Ouput error condition (and exit?) } //Continue success condition - no need for an elseHowever, I would also state that exit() is a poor way to handle error conditions. It almost always results in improperly formatted HTML pages. It doesn't take much to create an alternative path to produce a valid page with appropriate error conditions. Thanks, it’s a JSON response I am echoing out so using exit should be fine? Yeah your above example is spot on to what I am trying to do. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 6, 2018 Share Posted February 6, 2018 Thanks, it’s a JSON response I am echoing out so using exit should be fine? Possibly. exit() is typically overused for the wrong scenarios, but it does have its place. As long as your exit(s) ensure there will be no adverse effects, this may be one of those scenarios. 1 Quote Link to comment Share on other sites More sharing options...
mds1256 Posted February 6, 2018 Author Share Posted February 6, 2018 Possibly. exit() is typically overused for the wrong scenarios, but it does have its place. As long as your exit(s) ensure there will be no adverse effects, this may be one of those scenarios. Thanks for your response Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.