PHP - exit or return which is better?

后端 未结 4 715
日久生厌
日久生厌 2020-12-13 02:05

I would like to know in the following case which is a better option:

In the PHP script, if the $fileSize variable is larger than 100, I stop the script;

相关标签:
4条回答
  • 2020-12-13 02:18

    I would tend to go with the return() method, so that other scripts can continue executing. That way, if you ever use another script to call this one, it can do error-handling to deal with the case where the file is too large, as opposed to always halting execution.

    0 讨论(0)
  • 2020-12-13 02:32

    Since you are using exit and return within the global scope (not inside a function), then the behavior is almost the same.

    The difference in this case will appear if your file is called through include() or require(). exit will terminate the program, while return will take the control back to the calling script (where include or require was called).

    0 讨论(0)
  • 2020-12-13 02:32

    It depends...if your script is intended to do nothing else but output a message, and you don't want the script to do anything afterwards, exit() will work. Otherwise, use return.

    0 讨论(0)
  • 2020-12-13 02:34

    Exit terminates the program like die(). manual

    0 讨论(0)
提交回复
热议问题