8. php error reporting and debug

更新日期: 2021-06-29

介紹

  • 在 apache Configure File 內啟用 error reporting
  • 在 .htaccess 開啟 error reporting
  • 在 php code 內啟用 error reporting
  • 在 php.ini 內啟用 error reporting
  • Log error 去 file 去
  • print_r 與 var_dump
  • 小心 Disk Full
  • Write msg to log file - error_log
  • trigger_error()
  • Remark

 


在 Apache Configure File 內啟用 error reporting

 

/etc/httpd/conf/httpd.conf

php_value error_log none
php_flag display_errors On

Remark:

PHP 是以Apache module (mod_php) 執行時才有效 !!

 


在 .htaccess 開啟 error reporting

 

.htaccess

php_value error_reporting 7
php_flag display_errors On
php_flag display_startup_errors On

Remark:

Directory 要有 AllowOverride Options 的權限才可生效

<Directory /web/docs>
    AllowOverride Options
</Directory>

P.S.

php_flag is for on and off only, php_value is for values

 


在 php code 內啟用 error reporting

 

error_reporting()

// Turn off error reporting
error_reporting(0);

// Report runtime errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

ini_set()

# 在 webpage 上直接顯示 error. 建議改用 error_log()
ini_set('display_errors',1);

# 相當於 error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);                   

 


error_reporting value

 

# 這裡的 E_ALL 是 named constants 來

1         E_ERROR    # Execution of the script is halted.
2         E_WARNING  # Run-time warnings (non-fatal errors)
4         E_PARSE    # Compile-time parse errors
8         E_NOTICE   # '...PHP Notice:  Undefined variable:...'
...
2048      E_STRICT
...
32767     E_ALL

E_STRICT

Run-time notices, enable to have PHP suggest changes to your code which will ensure the best interoperability and

forward compatibility of your code.

More info.

http://www.php.net/manual/en/errorfunc.constants.php

 


在 php.ini 內啟用 error log

 

設定 log 什麼

; errors that occur during PHP's startup sequence
display_startup_errors = On 

; E_ALL = log all
; Default: E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting = E_ALL

; display err on browser
display_errors = On

 * 注意, 在 ini 檔內的設定是不用 ";" 結尾

Log error 去那裡

; 是否 log Err
log_errors = On

; default behavior: "empty"
; empty 時會 output 到 stderr
;error_log = syslog
error_log = php_errors.log

 


print_r 與 var_dump

 

 * pretty format

var_dump

Dumps information about a variable

print_r

Prints human-readable information about a variable

debug.php

<?php
    echo "<pre>";
    print_r($_SERVER);
    print_r($_COOKIE);
    print_r($_POST);
    print_r(file_get_contents('php://input'));
    echo "</pre>";
?>

$_SERVER

Every HTTP request header field is in $_SERVER (except Cookie)

所以要用

print_r($_COOKIE);

$_POST

它只可以 collect form data - "Content-Type: application/x-www-form-urlencoded"

ie. field1=value1&field2=value2

如果是 "Content-Type: text/plain", 它就係 Null 來, 所以要用

print_r(file_get_contents('php://input'));

 


try ... catch

 

try {
 ...
} catch (Exception $e) {
 echo $e->getMessage();
}

 

 


php "@" symbol

 

It's the error suppression operator. It will silent error messages.

Usage: before function/variable

ie.

<?php
/* Intentional file error */
$my_file = @file ('non_existent_file') or
    die ("Failed opening file: error was '" . error_get_last()['message'] . "'");
?>

 


小心 Disk Full

 

php code

// Turn off all error reporting
error_reporting(0);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

php.ini

; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT

Checking

<? var_dump(error_reporting()) ?>

int(22519)

22519 = 無 E_NOTICE(8), E_STRICT(2048), E_ALL(32767)

 


Write msg to log file - error_log

 

<?
    error_log("Debug: test test test");
?>

 

 


trigger_error()

 

using the PHP function trigger error

test.php

<?
trigger_error("test msg", E_USER_ERROR);
?>

log

... PHP Fatal error:  test msg in /path/to/test.php on line 2

E_USER_X

  • E_USER_ERROR
  • E_USER_WARNING
  • E_USER_NOTICE

 


Remark

 

  • Do NOT call session_start() before you call error_reporting()
  • parse errors ( missing ")" or ";" ) 是發生在 PHP execute 之前

 

 

 

Creative Commons license icon Creative Commons license icon