Hàm user_error

Hàm user_error (còn được biết đến với tên trigger_error) tạo ra một thông báo lỗi do người dùng định nghĩa. Tham số đầu tiên nhận chuỗi thông báo, tham số thứ hai - loại lỗi (E_USER_NOTICE, E_USER_WARNING hoặc E_USER_ERROR).

Cú pháp

phpuser_error(message, error_type);

Ví dụ

Tạo một thông báo đơn giản (E_USER_NOTICE):

php<?php
  user_error('This is a notice', E_USER_NOTICE);
?>

Kết quả thực thi mã:

phpNotice: This is a notice

Ví dụ

Tạo một cảnh báo (E_USER_WARNING):

php<?php
  user_error('Warning: invalid value', E_USER_WARNING);
?>

Kết quả thực thi mã:

phpWarning: Warning: invalid value

Ví dụ

Tạo một lỗi nghiêm trọng (E_USER_ERROR):

php<?php
  user_error('Critical error occurred', E_USER_ERROR);
  echo 'This line will not execute';
?>

Kết quả thực thi mã:

phpFatal error: Critical error occurred