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 함수,
    사용자 정의 오류 메시지를 생성합니다