调试时,如果我可以退出()并使用foreach打印数组中的单个元素,将会有所帮助。有任何想法吗?PHP调试:将foreach和exit组合起来
1
A
回答
2
如果您想轻松打印数组或其他PHP值的内容,请使用var_dump
。调用exit()
垂直于这一点,我认为这是很清楚的写:
var_dump($arr);
exit(1);
另一种方法是登录你的输出,这可能会造成更多的有用的,如果你不想通过你的输出HTML看过筛为var_dump
输出:
error_log(var_export($arr));
exit(1);
1
也许你可以做一个调试退出功能进行调试与print_r(打印变量的人类可读的版本)
function dexit($array, $status = 0) {
print_r($array);
exit($status);
}
那么,在你的代码的任何地方,你可以只
dexit($array);
// or
dexit($array, 0);
,但它并不难,只是使用的print_r在线两种方式:)
1
不听起来像调试恕我直言,最理想的做法,
http://php.net/manual/en/function.register-shutdown-function.php
如:
但是这可以通过使用register_shutdown_function()来实现
1
试试这个,你有两个选择,调试或后续代码var_dump
function debug_exit($array, $type = 1) {
if ($type == 1) { print_r($array); }
if ($type == 2) { var_dump($array); }
exit();
}
0
以下是我使用的是,x_r()
。跟踪来自this example在PHP文档。跟踪的原因是,您/他人可以找到哪个/什么x_r()
被调用通过。该exit()
是可选的,如果你需要看到主题下print_r()
一些原因:
// Exits with a print_r and call trace for debugging
if (!function_exists('x_r')) {
function x_r($obj, $exit = true, $return = true) {
// echo the obj first
echo '<pre style="background: #FFFFFF;">', print_r($obj, $return), '</pre>';
// include a debug call trace
$e = new Exception();
$trace = explode("\n", $e->getTraceAsString());
// reverse array to make steps line up chronologically
$trace = array_reverse($trace);
array_shift($trace); // remove {main}
array_pop($trace); // remove call to this method
$length = count($trace);
$result = array();
for ($i = 0; $i < $length; $i++) {
$result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
}
// echo call trace
echo '<pre style="background: #FFFFFF;">', print_r($result, $return), '</pre>';
if ($exit === true) {
exit();
}
}
}
相关问题
- 1. PHP将所有的值加起来foreach()
- 2. 如何将lambda和javascript forEach循环结合起来?
- 3. php数组和foreach?
- 4. 用PHP将PHP和PHP包装起来
- 5. c#exit generic ForEach使用lambda
- 6. PHP数组和foreach帮助
- 7. 调试foreach
- 8. foreach - > array - > json - 将它们组合在一起
- 9. PHP/MySql将排序和分页结合起来
- 10. exit()不起作用?
- 11. 将Gui和Processing结合起来
- 12. 将html和flash结合起来
- 13. 如何将`var`和`const`结合起来?
- 14. Maxscale:将readwritesplit和schemarouter结合起来
- 15. 如何将Catalyst和ngettext结合起来?
- 16. 将Lucene的WildcardQuery和FuzzyQuery结合起来
- 17. 将Get-Item和Get-ChildItem结合起来?
- 18. 将“ands”和“ors”组合起来的标准表达式
- 19. 将数组中的整数和字符串结合起来
- 20. 将Solr Q和FQ搜索组合起来
- 21. 调试QML和C++的组合
- 22. PHP - foreach数组
- 23. 数组合并将不会在foreach中工作php
- 24. 使用cURL和多个数组合并到多维的PHP foreach
- 25. 我可以将组合和继承与C#中的接口结合起来吗?
- 26. 如何将数组和地图结合起来并与之结合?
- 27. 将PHP脚本与另一个PHP文件,jQuery和HTML结合起来
- 28. 将R中的9列组合起来组成
- 29. 将PHP foreach循环与SQL foreach循环结合使用
- 30. PHP foreach数组ID
register_shutdown_function – dynamic 2011-02-26 01:14:51