php debug

Jun 23, 2016 pm 02:37 PM

1. 内置的API输出

echo/print

echo的速度较快, 且可以输出逗号分隔的多个变量

printf/sprintf/vprintf

用户格式化的一些输出或得到返回值

print_r/var_dump(var_export)/debug_zval_dump/debug_print_backtrace

输出数组,var_export输出的数组可以直接复制,debug_zval_dump包含引用次数,debug_print_backtrace可以让我们查看整个程序的调用栈,用来查看瞬间函数调用栈,方便在出错时查看执行上下文,不需要参数,直接调用

 

2. 错误选择控制和日志记录调试

Php.ini 配置中,跟错误相关的选项主要:error_reporting、display_errors、log_errors、error_log 等几个,这些选项在一般语法级别的调试是很有帮助的。

display_errors 是设定是否在PHP脚本执行输出的时候输出错误信息,一般在线下开发调试环境,为了便于调试,会打开错误显示选项,在线上为了不泄露敏感信息,一般会吧 display_errors 设置为Off。如果线上报错了,我们如何查看到错误?那么就需要 log_errors 选项了。这个选项是设定是否记录错误日志,以及error_log指明日志的路径。

 

错误抛出和处理,主要是说我们在程序中能够自己触发错误,或者自己截获处理错误,类似 throw catch机制。
trigger_error, set_error_handler, set_exception_handle这三个api就是处理错误抛出和处理内置函数。
trigger_error可以触发一个错误,触发基本跟上文的error_reporting设定的级别一直,主要是能够触发E_USER_ERROR,
E_USER_WARNING, E_USER_NOTICE三种级别的错误,如果不做处理,程序执行就会报错,错误类似于error_reporting,但是页面不能显示,但是cli执行可以显示。

set_error_handler 说我们发生了错误的时候,用什么处理函数来处理,一般这个都是用来针对 trigger_error 之后来进行错误处理的,两个函数结合,我们可以构建一个简单有效的错误识别和记录的功能。会传递参数errno errstr errfile errline
Set_exception_handler 跟 set_error_handler 类似,不过 Set_exception_handler 是用来处理出现未捕获的异常之后需要调用的处理方法。


日志记录除了PHP解析级别的错误,更多是我们程序在执行过程中的一些错误,比如 文件资源打开错误(文件不存在、没有权限、文件格式不正确)、远程服务资源访问失败(网络不通、协议不正确、用户名密码错误)等等,要知道,任何你认为不会出错的地方都可能隐藏着错误,所以务必多多的输出Log。
可以写一个简单的日志记录类,其实就是记到不同的文件中。
写log大抵都是几个常用的文件操作API,比如 fopen/fwrite,或者是一步到位的 file_put_contents。另外,PHP为了便于写日志,还提供了一个专门的接口:error_log

 

3. 浏览器调试

如果Web前端调试来说,Firebug是不可或缺好的调试工具,它能够监控网络、监测css、js错误,查看DOM节点,FirePHP是一个附加在 firebug 上面的插件,用来调试PHP,操作过程很简单。在PHP端使用FirePHP提供的PHP日志记录类库来输出调试信息, 在浏览器端使用 Firebug + FirePHP 来接收查看输出的调试信息,这些调试信息会直接附加在返回的HTTP头信息里,这些信息不会被浏览器直接显示,只会在firephp 获取显示,有效的达到了调试和页面显示都不冲突的问题。(必须使用firefox浏览器)。要在服务器端部署FirePHPCore文件,然后就可以调用提供的方法,在header中输出一些变量,可以有各种group,table和展现效果(warn info error)。可以查看trac, exception。参考http://www.firephp.org/HQ/Use.htm了解更多信息

 

4. IDE调试

zend支持本地调试,设置断点,步步跟踪,同时可以安装zend Debugger 进行远程调试。

ZendDebugger 是一个Zend扩展,通过跟IDE进行通信来达到设置断点和调试的目的。基本工作原理是:IDE 会设置监听一个调试端口,在调试的时候设置一个远程Web服务器地址,调试的时候触发URL信息,通知调试器 ZendDebugger 来访问 IDE 开放的调试端口,双方连接上以后,开始互相依靠传递消息来进行调试工作,知道调试完成,网路连接断开。但对基于ZendFramework的程序似乎不太管用,zf内嵌了zend_debug,或许可以看一下。

 

5. APD(Advanced PHP Debugger)

......

 

6. 使用Xhprof 进行性能分析

.....

 

 

 

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1247
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles