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

11 replies
Which function is used for terminate the script execution in PHP?
#execution #function #php #script #terminate
  • {{ DiscussionBoard.errors[6297327].message }}
  • Profile picture of the author htmlthis
    You can use die("Error message goes here if any");
    {{ DiscussionBoard.errors[6301498].message }}
  • Profile picture of the author ionutcib
    Use "die" if there is an error of "exit" if you simply want to terminate normally your script.
    Signature
    {{ DiscussionBoard.errors[6304586].message }}
  • Profile picture of the author Terry Crim
    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.
    {{ DiscussionBoard.errors[6317209].message }}
    • Profile picture of the author htmlthis
      Originally Posted by Terry Crim View Post

      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 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.
      {{ DiscussionBoard.errors[6318784].message }}
      • Profile picture of the author Terry Crim
        Originally Posted by htmlthis View Post

        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.
        I just simply stated I have never seen it used in that way. None of the tutorials or books I have ever read use it outside of database related operations.

        I looked it up in the manual it is an equivalent to exit();

        If you use it and it works great. I don't.

        exit(1); will return error codes, anything other than a 0 is considered a failure.

        Instead of using die, if you are using it to display and error to the user, it would actually be better to create an error routine function or class that handles errors. This way you can provide a fall back that would work instead.

        My thoughts on this. If die, works for you then use it.
        {{ DiscussionBoard.errors[6327111].message }}
  • Profile picture of the author Manfred Ekblad
    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.
    {{ DiscussionBoard.errors[6327192].message }}
  • Profile picture of the author SteveSRS
    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).
    {{ DiscussionBoard.errors[6327485].message }}
    • Profile picture of the author annaharris
      Use "die" if there is an error of "exit" if you simply want to terminate normally your script.
      {{ DiscussionBoard.errors[6365121].message }}
      • Profile picture of the author bugtrack
        You should use exit function to terminate the script execution in php
        {{ DiscussionBoard.errors[6369948].message }}
  • Profile picture of the author JohnnyS
    like Manfred Ekblad said..

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

    i both use them in different cases..
    {{ DiscussionBoard.errors[6370767].message }}

Trending Topics