Home

Share via:

PHP: Stop Code Execution with Exit

How we can use the PHP's exit function to stop the execution of remaining code.

456 views

Edited: 2020-02-28 00:17

Stop running the script with the exit function, PHP

The exit function allows us to stop code-execution from anywhere within our scripts.

It is common to call the exit function after showing an error message from within a function or method, often because a page (web resource) is not found.

Other times, it is used for quick and dirty bug hunting by developers.

When exit(); is called from within a function, PHP will stop executing the rest of the script.

A common place to call exit is after sending a location header redirect:

header('Location: https://beamtic.com/');
exit();

Calling exit from functions and methods

When a function or method is called, it will usually return to the calling script once it finishes executing the relevant function-code.

If for example we responding to a HTTP request from within a function, it would often be desired to have the script stop executing any remaining code. I.e.:

function respond_not_found($code) {
  http_response_code(404);
  header('Content-Type: text/html; charset=UTF-8');
  echo '<h1>404 Not Found</h1>' . 
       '<p>Eh!? Now you are just guessing...</p>';
  exit();
}

Then, when checking the requested page, we can simply output a 404 and exit when the page is not found:

$valid_system_pages_arr = array('frontpage' => true, 'categories' => true, 'blog' => true);

if (isset($valid_system_pages_arr["$requested_page"]) == false) {
  respond_not_found();
}

// The rest of our code goes here :-D
echo 'A valid system page was requested.';

Exiting from within loops

We can not use exit inside a loop, since it would completely stop running the rest of the program. Usually, you would want to just stop executing the rest of the loop, for this we can use the break keyword.

However, using the break keyword would usually rise the need to use conditional logic inside of the loop, which can be inefficient, depending on the circumstances.

Using the break keyword to stop a loop from running:

$i = '1';
while ($number <= '15') {
  break(); // Only the loop stops executing

  echo 'Run Number:' . $number . "\n";
  ++$number;
}

The below would not work:

$i = '1';
while ($number <= '15') {
  exit(); // The program exits on the first run without running any remaining code

  echo 'Run Number:' . $number . "\n";
  ++$number;
}

// This code will not be executed, since we "exit" the script inside the loop

Bug hunting with exit

Using exit for bug hunting can work well at times, but as an application grows, this becomes increasingly more tedious.

Often when PHP developers are debugging applications, they will be using var_dump and print_r in combination with exit to output the content of variables and objects. This can be practiced from anywhere within a script, even from methods in a Object Orientated application.

An example of this would be:

$my_array = array('bug', 'hunt', 'professionals');
$some_variable = 'test';
var_dump($some_variable);
print_r($my_array);
exit();

Comments