14,953
edits
(→檢查步驟) |
|||
| Line 1: | Line 1: | ||
PHP 技術問題的排除與解決 | PHP 技術問題的排除與解決 | ||
# | == Troubleshooting steps of PHP errors == | ||
Display error message or check the PHP error logs | |||
* {{kbd | key=<nowiki>ini_set("display_errors", "On");</nowiki>}} See details on [http://php.net/manual/en/errorfunc.configuration.php PHP: Runtime Configuration - Manual] | |||
* {{kbd | key=<nowiki>error_reporting(E_ALL);</nowiki>}} See details on [http://php.net/manual/en/function.error-reporting.php PHP: error_reporting - Manual] | |||
* [https://www.jetbrains.com/help/phpstorm/event-log-tool-window.html Event Log - Help | PhpStorm] | |||
Check the Web service logs | |||
# {{kbd | key=<nowiki>sudo tail /var/log/httpd/error.log</nowiki>}} if the web service logs was located at {{kbd | key=<nowiki>/var/log/httpd/error.log</nowiki>}} | |||
# Example log: {{kbd | key=<nowiki>[Thu Nov 08 12:20:40.412630 2018] [:error] [pid 12769] [client x.x.x.x:4433] PHP Parse error: syntax error, unexpected '$var' (T_VARIABLE) in /home/wwwroot/path/to/file.php on line 100</nowiki>}} | |||
# Display partial source code: {{kbd | key=<nowiki>sed -n '990,110p;111q' /home/wwwroot/path/to/file.php</nowiki>}}<ref>[https://stackoverflow.com/questions/83329/how-can-i-extract-a-predetermined-range-of-lines-from-a-text-file-on-unix How can I extract a predetermined range of lines from a text file on Unix? - Stack Overflow]</ref> | |||
# 檢查網站伺服器 error log e.g. 如果使用 [https://www.nginx.com/ NGINX] Web Server 可輸入指令 {{kbd | key=<nowiki>sudo tail /var/log/nginx/error.log</nowiki>}} | # 檢查網站伺服器 error log e.g. 如果使用 [https://www.nginx.com/ NGINX] Web Server 可輸入指令 {{kbd | key=<nowiki>sudo tail /var/log/nginx/error.log</nowiki>}} | ||
Simply the question | |||
* Is the [http://php.net/manual/en/function.function-exists.php function_exists]? | |||
** Extension dependency? Install the extension you need. | |||
** If not, looking for the alternative functions. | |||
* Was the function really executed? Maybe using the __LINE__ to examin the logic. | |||
* Disable the logical judgement and just print text using [http://php.net/manual/en/function.var-dump.php var_dump] or [http://php.net/manual/en/function.print-r.php print_r] functions. | |||
* Check the if-else condition was satisfied or not cf: [http://www.leepoint.net/notes-java/data/strings/12stringcomparison.html Java: String Comparison] It's logical (compiler will not show the warning) but wrong. | |||
* try & catch: [http://php.net/manual/en/language.exceptions.php PHP: Exceptions] | |||
=== logging === | |||
native error logging | |||
* [http://www.php.net/ PHP] log: check the configuration file: /etc/php.ini (the location of configuration file can be verified by [http://php.net/manual/en/function.phpinfo.php phpinfo()] ) | |||
(for production site) unmark theese lines in the php.ini and restart Apache service | |||
<pre> | |||
log_errors = On | |||
error_log = "php_error.log" | |||
</pre> | |||
(for development site) using [http://php.net/manual/en/function.error-reporting.php error_reporting] | |||
<pre> | |||
error_reporting(E_ALL); | |||
</pre> | |||
[http://tw1.php.net/print_r PHP: print_r - Manual] | |||
<pre> | |||
$log = print_r($variable, true); | |||
//save to the log file | |||
$log = print_r(debug_backtrace(), true); | |||
//Generates a backtrace and save to the log file | |||
</pre> | |||
capture the result of var_dump: | |||
ob_start | |||
* [http://stackoverflow.com/questions/139474/how-can-i-capture-the-result-of-var-dump-to-a-string php - How can I capture the result of var_dump to a string? - Stack Overflow] | |||
* [http://php.net/manual/en/function.error-log.php PHP: error_log - Manual] | |||
<pre> | |||
ob_start(); | |||
var_dump($some_variable); | |||
$result = ob_get_clean(); | |||
error_log($result, 3, 'd:/result.log'); | |||
</pre> | |||
QuickForm | |||
<pre> | |||
$result = var_dump($some_variable); | |||
$form->addElement('html', $result); | |||
</pre> | |||
[http://ellislab.com/codeigniter CodeIgniter]: [http://stackoverflow.com/questions/7370391/how-to-configure-codeigniter-to-report-all-errors php - How to configure Codeigniter to report all errors? - Stack Overflow] | |||
show the line number and filename | |||
<pre> | |||
echo 'Houston, we've had a problem '. __line__ . ' ' . __FILE__ ."<br />"; | |||
</pre> | |||
more on [http://php.net/manual/en/language.constants.predefined.php PHP: Magic constants], [https://github.com/Seldaek/monolog monolog] | |||
== Configuration and Environment Issues == | == Configuration and Environment Issues == | ||