如何调试PHP脚本?
我知道基本的调试,如使用错误报告。PHPEclipse中的断点调试也非常有用。
在phpStorm或任何其他IDE中调试的最佳方法(就快速和简单而言)是什么?
如何调试PHP脚本?
我知道基本的调试,如使用错误报告。PHPEclipse中的断点调试也非常有用。
在phpStorm或任何其他IDE中调试的最佳方法(就快速和简单而言)是什么?
当前回答
XDebug对于开发至关重要。我安装它之前任何其他扩展。它为您提供任何错误的堆栈跟踪,您可以轻松地启用分析。
使用var_dump()可以快速查看数据结构。不要使用print_r(),因为你必须用<pre>包围它,而且它一次只打印一个var。
<?php var_dump(__FILE__, __LINE__, $_REQUEST); ?>
对于一个真正的调试环境,我发现最好的是Komodo IDE,但它要花费$$。
其他回答
我使用了Zend Studio(5.5)和Zend平台。这就提供了适当的调试、断点/跨代码等,尽管是有代价的。
这是我的调试环境:
error_reporting(-1);
assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_BAIL, 0);
assert_options(ASSERT_QUIET_EVAL, 0);
assert_options(ASSERT_CALLBACK, 'assert_callcack');
set_error_handler('error_handler');
set_exception_handler('exception_handler');
register_shutdown_function('shutdown_handler');
function assert_callcack($file, $line, $message) {
throw new Customizable_Exception($message, null, $file, $line);
}
function error_handler($errno, $error, $file, $line, $vars) {
if ($errno === 0 || ($errno & error_reporting()) === 0) {
return;
}
throw new Customizable_Exception($error, $errno, $file, $line);
}
function exception_handler(Exception $e) {
// Do what ever!
echo '<pre>', print_r($e, true), '</pre>';
exit;
}
function shutdown_handler() {
try {
if (null !== $error = error_get_last()) {
throw new Customizable_Exception($error['message'], $error['type'], $error['file'], $error['line']);
}
} catch (Exception $e) {
exception_handler($e);
}
}
class Customizable_Exception extends Exception {
public function __construct($message = null, $code = null, $file = null, $line = null) {
if ($code === null) {
parent::__construct($message);
} else {
parent::__construct($message, $code);
}
if ($file !== null) {
$this->file = $file;
}
if ($line !== null) {
$this->line = $line;
}
}
}
根据问题的不同,我喜欢将error_reporting(E_ALL)与echo测试混合使用(以找到最初发生错误的违规行/文件;你知道它并不总是行/文件php告诉你对吗?),IDE大括号匹配(解决“解析错误:语法错误,意外的$end”问题),和print_r();退出;转储(真正的程序员查看源代码;p)。
你也不能用“memory_get_usage();”和“memory_get_peak_usage();”来打败phpdebug(检查sourceforge)来找到问题区域。
Xdebug和用于notepad++的DBGp插件用于繁重的bug查找,FirePHP用于轻量级的东西。又快又脏?没有什么比dBug更好的了。
我使用内置调试器的zend studio for eclipse。与使用eclipse pdt和xdebug进行调试相比,它仍然很慢。希望他们能解决这些问题,在最近的版本中,速度有所提高,但仍然需要2-3秒。 zend firefox工具栏让事情变得非常简单(调试下一页、当前页等)。此外,它还提供了一个分析器,可以对代码进行基准测试,并提供饼状图、执行时间等。