error_reporting(PHP 4, PHP 5)
error_reporting — Sets which PHP errors are reportedDescriptionint error_reporting ([ int $level ] )
The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script.
Parameterslevel
The new error_reporting level. It takes on either a bitmask, or named constants. Using named constants is strongly encouraged to ensure compatibility for future versions. As error levels are added, the range of integers increases, so older integer-based error levels will not always behave as expected.
The available error level constants and the actual meanings of these error levels are described in the predefined constants.
Return ValuesReturns the old error_reporting level.
<?php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);
// Report all PHP errors (see changelog)
error_reporting(E_ALL);
// Report all PHP errors
error_reporting(-1);
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
?>
error_reporting(0);- ปิด error report ก็มีผลดีเหมือนกัน ตรงที่ หากระบบของคุณ วันดีคืนดีมันเกิดเสียหาย หรือมี BUG มันจะไม่แจ้ง error ให้กับผู้ใช้เห็น แต่มันจะทำงานต่อไม่ได้เช่นกัน
error_reporting(E_ERROR | E_WARNING | E_PARSE);- เป็นการแสดง error เฉพาะในส่วนที่ run อยู่เท่านั้น จะไม่สนใจกับส่วนอื่น อันนี้ไม่น่าสนใจเท่าตัวต่อไปครับ
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);- ตัวนี้น่าสนใจมากตรงที่มันจะเตือนเมื่อคุณ
"ตั้งชื่อตัวแปร" ไม่ถูกตามกฏ หรือมีส่วนที่ไม่เหมาะสม มันจะเตือนขึ้นมาหมดเลยครับ
* แนะนำให้ใช้ (Recommend)error_reporting(E_ALL ^ E_NOTICE);- ค่า default มันเป็นแบบนี้อยู่แล้ว ไม่น่าสนใจอะไร
error reporting ปกติ จะไม่แสดง error ถ้าโปรแกรมที่เราเขียนมันไม่ error จริงๆ หรือหยุดการทำงาน หรือมีการติดขัดในการทำงาน มันจะไม่แจ้ง error หรือ เตือนอะไรทั้งสิ้น ดังภาพ
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); หลังจากเปิด แบบเต็มประสิทธิภาพแล้ว ผลออกมาเป็นแบบภาพด้านล่างนี้ แทบไม่เชื่อเลยใช่มั้ย ว่า code ที่เราเขียนไม่ error แต่มี "การเตือน" มากมายขนาดนี้
ดูกันชัดๆ
undefined index d ก็หมายถึง มันไม่พอค่าของตัวแปร d หรือค่าเป็น NULL นั่นเอง
use undefined of constant ..... - assumed '.....' in ก็หมายถึง ....ตัวแปร... ของเรา ไม่ได้กำหนดเครื่องหมาย ' ' หรือ " " ไว้
ตัวอย่างเช่น มีตัวแปรรับค่ามาจาก
form ด้วยการ
post ชื่อตัวแปรที่ใช้คือ
$_POST[dekitclub] ซึ่งที่ถูกต้อง คือ
$_POST['dekitclub'] $_POST[dekitclub] // Not bad
$_POST['dekitclub'] // Excellent!
$dekitclub // Noob
หรือ
$_SESSION[username] ที่ถูกต้องคือ
$_SESSION['username'] $_SESSION[username]
$_SESSION['username']
ใครที่ใช้รูปแบบนี้ระวังให้ดีนะครับ (เห็นใช้กันเยอะ)
"hello $member"; มันจะเตือนคุณโดยไม่รู้ตัว!