I've noticed people using exit; with the header() function. And others don't.

For example:

<? header("Location: http://www.somewhere.com"); exit;  ?>

Does anyone know why? I've tried understanding what people are explaining at http://us2.php.net/exit but I'm getting confused.

Is it best practice to use exit; when redirecting?

    Doing a location header() does not immediately perform the redirect. Rather, it queues up that header along with any other HTTP headers. Thus, if you want the redirect to happen at that point in the code and ensure that nothing else in the script after that gets executed, then the exit() does so.

      As a side topic note that using exit with integers acts differently than strings. A long time ago I programmed a submission script that would terminate with various status codes. As a coding shortcut instead of doing this:

      echo $code;
      exit;

      I just did this

      exit($code);

      Sometimes the codes would output ok, sometimes not... this really confused me before I found out that if the value was a string it would output it, otherwise the program would quit with that status code - useless as far as my script was concerned.

        Write a Reply...