กรุณาปิด AdBlock!

Cancel your adBlock please.

ขยายหน้าเว็บRegister Login
 โฆษณา
หน้า: [1]  ลงล่าง
  พิมพ์  
topic

PHP - ข้อดีของการตั้งค่า error reporting  (อ่าน 16324 ครั้ง)

ไอที
« เมื่อ: 25, 02 2010, 09:08:38 AM »
error_reporting
(PHP 4, PHP 5)

error_reporting — Sets which PHP errors are reported

Description
int 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.

Parameters

level
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 Values
Returns 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(&#39;error_reporting&#39;, 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 php"

error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE); หลังจากเปิด แบบเต็มประสิทธิภาพแล้ว ผลออกมาเป็นแบบภาพด้านล่างนี้ แทบไม่เชื่อเลยใช่มั้ย ว่า code ที่เราเขียนไม่ error แต่มี "การเตือน" มากมายขนาดนี้

"error reporting php"

ดูกันชัดๆ

"error reporting php"

undefined index d ก็หมายถึง มันไม่พอค่าของตัวแปร d หรือค่าเป็น NULL นั่นเอง

use undefined of constant ..... - assumed '.....' in ก็หมายถึง ....ตัวแปร... ของเรา ไม่ได้กำหนดเครื่องหมาย ' ' หรือ " " ไว้

ตัวอย่างเช่น มีตัวแปรรับค่ามาจาก form ด้วยการ post ชื่อตัวแปรที่ใช้คือ $_POST[dekitclub] ซึ่งที่ถูกต้อง คือ $_POST['dekitclub']

 $_POST[dekitclub// Not bad
 
$_POST[&#39;dekitclub&#39;] // Excellent!
 
$dekitclub // Noob
หรือ

$_SESSION[username] ที่ถูกต้องคือ $_SESSION['username']
 $_SESSION[username]
 
$_SESSION[&#39;username&#39;]

ใครที่ใช้รูปแบบนี้ระวังให้ดีนะครับ (เห็นใช้กันเยอะ) "hello $member"; มันจะเตือนคุณโดยไม่รู้ตัว!
เด็กไอทีคลับ
เด็กไอทีตัวพ่อ
เด็กไอทีคลับชั้นเซียน
*
พลังความคิด 89
กระทู้: 3,536
บันทึกการเข้า
ไอที
« ตอบ #1 เมื่อ: 25, 02 2010, 09:48:51 AM »
สามีใครหว่าสุดยอด
CoolPha
RUK-YOM
ปรมาจารย์ด้านไอที
*
พลังความคิด 76
กระทู้: 5,261
เว็บไซต์
บันทึกการเข้า

ไอที
« ตอบ #2 เมื่อ: 13, 04 2010, 02:48:19 AM »
ได้ความรู้คับ
KMUTNB
เด็กไอทีคลับไฟแรง
*
พลังความคิด 9
กระทู้: 118
บันทึกการเข้า
หน้า: [1]  ขึ้นบน
  พิมพ์  
 
กระโดดไป: