The Exit Construct

The exit construct (or its alias die) immediately halts the execution of a PHP script. It can accept an optional parameter - a message or a numeric status that will be output before termination.

Syntax

phpexit;
// or
exit(message);

Example

Simple script termination:

php<?php
  echo 'This will be executed';
  exit;
  echo 'This will NOT be executed';
?>

Code execution result:

php'This will be executed'

Example

Termination with a message:

php<?php
  exit('Script terminated');
?>

Code execution result:

php'Script terminated'

Example

Usage in a conditional structure:

php<?php
  $res = [1, 2, 3];
  
  if (empty($res)) {
    exit('Array is empty');
  }
  
  print_r($res);
?>

Code execution result:

php[1, 2, 3]

See Also

  • the trigger_error function,
    which generates a user-defined error message