Fungsi trigger_error

Fungsi trigger_error menjana mesej ralat pengguna. Parameter pertamanya menerima teks mesej, dan yang kedua - tahap ralat (lalai E_USER_NOTICE). Fungsi ini berguna untuk penyahpepijat dan mencipta sistem pengendalian ralat sendiri.

Sintaks

phptrigger_error(message, error_level);

Contoh

Jana notis mudah:

php<?php
  trigger_error("This is a notice message");
?>

Hasil pelaksanaan kod:

phpNotice: This is a notice message in file.php on line 2

Contoh

Jana amaran pengguna:

php<?php
  trigger_error("Invalid value entered", E_USER_WARNING);
?>

Hasil pelaksanaan kod:

phpWarning: Invalid value entered in file.php on line 2

Contoh

Jana ralat maut:

php<?php
  trigger_error("Critical system error", E_USER_ERROR);
?>

Hasil pelaksanaan kod:

phpFatal error: Critical system error in file.php on line 2

Contoh

Penggunaan dalam struktur bersyarat:

php<?php
  $age = -5;
  if ($age < 0) {
    trigger_error("Age cannot be negative", E_USER_WARNING);
  }
?>

Hasil pelaksanaan kod:

phpWarning: Age cannot be negative in file.php on line 4