Trong bài này, chúng ta sẽ tìm hiểu về cách xử lý lỗi (error) trong PHP. Để học tốt bài này, các bạn cần đọc lại bài Cài đặt môi trường lập trình Web PHP với XAMPP để biết cách chạy ứng dụng web PHP với XAMPP. Show Nội dung chính
Nội dung chính
1. Sử dụng hàm die() và exit() trong PHP1.1. Hàm die() trong PHPHàm die() giúp in thông báo lỗi và không làm dừng chương trình. Cú pháp của hàm die(): die($message);Ví dụ, đoạn code mở file bên dưới sẽ bị lỗi vì không tồn tại file gochocit.txt. <?php $file=fopen("gochocit.txt","r"); ?>Kết quảWarning: fopen(gochocit.txt): Failed to open stream: No such file or directory in C:\xampp\htdocs\gochocit\index.php on line 2Chúng ta có thể ngăn lỗi bằng cách kiểm tra file có tồn tại hay không và sử dụng hàm die() để đưa ra thông báo về lỗi. <?php if(!file_exists("gochocit.txt")) { die("File is not exist!"); } else { $file = fopen("gochocit.txt", "r"); } ?>Kết quảFile is not exist!1.2. Hàm exit() trong PHPHàm exit() trong PHP giúp xuất một thông báo và kết thúc (thoát) script hiện tại. Cú pháp: exit(string $status = ?): void exit(int $status): voidNếu status là một string thì sẽ in ra thông báo trước khi thoát. Nếu status là kiểu int (từ 0 đến 254) thì sẽ không in gì cả và thoát. <?php //exit program normally exit; exit(); exit(0); //exit with an error code exit(1); exit(0376); //octal // exit and print message $filename = 'gochocit.com'; $file = fopen($filename, 'r') or exit("unable to open file ($filename)"); ?>2. Tùy chỉnh xử lý lỗi (custom error handler) trong PHPKhi một lỗi xảy ra, chúng ta có thể tùy chỉnh xử lý lỗi và thông báo lỗi. Để làm việc này, chúng ta cần định nghĩa một hàm xử lý lỗi với cú pháp như sau: error_function($error_level, $error_message, $error_file, $error_line, $error_context)Trong đó,
Khi lỗi xảy ra, PHP sẽ tự xử lý lỗi theo cách của nó. Chúng ta cần sử dụng hàm set_error_handler() để thiết lập tùy chỉnh xử lý lỗi theo ý của mình. Cú pháp của hàm set_error_handler(): set_error_handler("error_function");với error_function là tên của hàm xử lý lỗi mà chúng ta tự định nghĩa. <?php //error handler function function customError($errno, $errstr, $errfile, $errline) { echo "<b>Error:</b> [$errno]: $errstr <b>in</b> $errfile <b>at line</b> $errline"; } //set error handler set_error_handler("customError"); //This will generate error echo($test); ?>Kết quảError: [2]: Undefined variable $test in C:\xampp\htdocs\gochocit\index.php at line 93. Hàm trigger_error() trong PHPCó những trường hợp, người dùng nhập những giá trị không hợp lệ. Chúng ta có thể sử dụng hàm trigger_error() để thông báo lỗi. <?php $test=-1; if ($test<0) { trigger_error("Value must be 0 or above"); } ?>Chúng ta cũng có thể sử dụng hàm trigger_error() với tùy chỉnh xử lý lỗi. Ví dụ: <?php //error handler function function customError($errno, $errstr, $errfile, $errline) { echo "<b>Error:</b> [$errno]: $errstr <b>in</b> $errfile <b>at line</b> $errline"; die(); } //set error handler set_error_handler("customError", E_USER_WARNING); //trigger error $test=2; if ($test>=1) { trigger_error("Value must be 1 or below", E_USER_WARNING); } ?>Kết quảError: [512]: Value must be 1 or below in C:\xampp\htdocs\gochocit\index.php at line 144. Hàm error_log() trong PHPMặc định, PHP sẽ gửi error logs (có thể gọi là nhật ký lỗi) đến hệ thống ghi log trên server hoặc một file nào đó. Việc này tùy thuộc vào cấu hình error_log trong file php.ini. Nhưng nếu sử dụng hàm error_log() có thể giúp chúng ta gửi error logs đến nơi mà chúng ta cần lưu trữ. Ví dụ, chúng ta muốn ghi error logs đến vào my-errors.log trong PHP. <?php //error handler function function customError($errno, $errstr) { echo "<b>Error:</b> [$errno] $errstr<br>"; echo "Webmaster has been notified"; //write error log to my-errors.log file error_log("You messed up!\n", 3, "my-errors.log"); } //set error handler set_error_handler("customError",E_USER_WARNING); //trigger error $test=2; if ($test>=1) { trigger_error("Value must be 1 or below",E_USER_WARNING); } ?>Hàm error_log() có thể giúp gửi error logs đến mail hoặc một file nào khác. Các bạn có thể tham khảo thêm tại error_log PHP.
|