[Solved] is it bad practice to kill a script inside of a function?


That is perfectly safe, if that is your question, since php as a quasi interpreted language does not leave any traces or artifacts when terminating the execution.

If it is a good practice is another thing. I’d say it is fine for testing purposes, but you should avoid it in final code. Reason is that it makes code hard to maintain. Consider someone else diving into your code and trying to understand the logic. One would actually have to go through all the code to stumble over this detail. Chances is one does not, so the behavior of your code appears broken.

Instead try to do one of these:

  • throw an exception to leave the current scope. Such an exception might well be caught and swallowed by the calling scope, but it is a clear and predictable way of returning. Obviously you should document such behavior.

  • return a value clearly out of scope to what would “normally” be returned. So for example null or false instead of a typical value. This forces the calling scope to check the return value, but that is good practice anyway.

  • restructure your code such that there is no reason to suddenly terminate the execution.

3

solved is it bad practice to kill a script inside of a function?