exit コンストラクション

コンストラクション exit (またはそのエイリアス die) は、PHPスクリプトの実行を直ちに停止します。 オプションのパラメータ(終了前に出力されるメッセージまたは数値ステータス)を取ることができます。

構文

phpexit;
// または
exit(message);

例

スクリプトの単純な終了:

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

コード実行結果:

php'This will be executed'

例

メッセージを出力して終了:

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

コード実行結果:

php'Script terminated'

例

条件分岐内での使用:

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

コード実行結果:

php[1, 2, 3]

関連項目

  • 関数 trigger_error,
    これはユーザー定義のエラーメッセージを生成します