Which function is used for terminate the script execution in PHP?

by 11 replies
14
Which function is used for terminate the script execution in PHP?
#programming #execution #function #php #script #terminate
  • You can use die("Error message goes here if any");
  • Use "die" if there is an error of "exit" if you simply want to terminate normally your script.
  • I have never seen "die" used outside of a database scenerio, input or output, connecting to db etc... I don't think any of the books I have use that particular call outside these scenerios even.

    There are variables you can put inside exit() to get error codes etc... back but personally in the situations I use exit, there wouldn't be any error codes due to how I write my code.

    You can just type:

    Code:
    exit;
    It will stop execution of the program. If you are trying to exit out of a loop, depending on how many levels you are in you can just use:

    break;

    Which will break out of the current loop and either exit the loop OR break out of the level it is currently in and go up one level in the looping sequence you have.

    If you code recursion loops it is good idea to use "break" to limit or prevent infinite loops.
    • [1] reply
    • There are quite a lot of people that use die(), and there's more than one way to skin a cat. Just because you're not using it does not make it wrong.
      • [1] reply
  • die() is just an alias for exit() so there is absolutely no difference in how to use it or what it does.

    So die() is not only equivalent to exit(), it is actually the exact same function just with a different name.

    PHP: List of Function Aliases - Manual

    Personally I prefer to always use the master functions instead of their aliases.
  • Manfred is right.. however using any of these functions is not too much recommended in my opinion.. is 95% of the cases there is always a better solution and more user friendly solution.

    I always use my own defined function where I nicely include the footer (to make sure the page still looks normal) displaying the error and then offering alternative places to go (and of course logging for me to fix the error).
    • [1] reply
    • Use "die" if there is an error of "exit" if you simply want to terminate normally your script.
      • [1] reply
  • like Manfred Ekblad said..

    die and exit has no difference die is just an alias of exit..

    i both use them in different cases..

Next Topics on Trending Feed