调试PHP程序的多种方法介绍_php技巧
调试的定义:通过一定方法,在程序中找到并减少缺陷的数量,从而使其能正常工作。
这里说一些如何调试PHP程序的经验。
一、PHP自带的调试功能
1、自带的报错功能
两个名词:开发环境是开发人员在进行开发和调试的环境,生产环境是最终客户在用的线上环境;
开发环境和生产环境要分开设置报错功能。
(1)开发环境
开发环境需要打开报错,以下是php.ini的配置项及其说明:
; This directive sets the error reporting level.
; Development Value: E_ALL | E_STRICT (Show all errors, warnings and notices including coding standards.)
error_reporting = E_ALL | E_STRICT
; This directive controls whether or not and where PHP will output errors,
; notices and warnings too. Error output is very useful during development.
; Development Value: On
display_errors = On
这样你在开发过程中,能第一时间发现错误。
即使是一个低等级的报错“Notice: Undefined variable: a in E:\phpspace\test.php on line 14”,但一个未定义的变量的使用往往暗藏着bug。
你会问,如果我引进了开源的类库,他们抛出一堆的低等级错误怎么办?一般代码质量好的类库,也没有“Notice”级别的报错的。所以这也是鉴别一个类库质量的方法。
(2)生产环境
生产环境不能直接将错误输出,而是记入日志,以下是php.ini的配置项及其说明:
; It could be very dangerous in production environments.
; It's recommended that errors be logged on production servers rather than
; having the errors sent to STDOUT.
display_errors = Off
; Besides displaying errors, PHP can also log errors to locations such as a
; server-specific log, STDERR, or a location specified by the error_log
; directive found below. While errors should not be displayed on productions
; servers they should still be monitored and logging is a great way to do that.
; Production Value: On
log_errors = On
; Log errors to specified file.
error_log = /path/to/php_error.log
当然日志写到文件里只是一个选择,还有其他配置可参考手册。
生产环境是给客户提供服务的,你不可能在上面进行断点、打印输出等操作,所以日志是不错的选择。
2、其他一些语言特性、功能的使用
(1)少用错误控制运算符“@”
其的作用是,将“@”放置在一个PHP表达式之前,该表达式可能产生的任何错误信息都被忽略掉。
如果一个缺陷发生在这个表达式中,从PHP的输出中看不到任何错误,这增加了调试的难度。所以能不用则不用。
(2)有些函数自带有debug功能
比如这行代码:
$fp = fsockopen(“www.example.com”, 80, $errno, $errstr, 30);
开发者调试时已经确定,$fp为空,连接失败,是这一行有问题,但是为什么连接失败?
函数是php自带的,无法进行更深入的调试。所以一般这样的函数(主要是网络通信类的),会自己提供调试参数:$errno和$errstr。你可以加一句:
if (!$fp) echo “$errstr ($errno)
\n”;
就能看到连接失败的原因了。
这些函数有:fsockopen,pfsockopen,stream_socket_server,stream_socket_client 等。
还有些函数是调试一个功能用的,比如:mysql_errno,socket_last_error,socket_strerror 等。
这些只需要了解下,碰到能想到用即可。
二、引进调试工具
在遇到复杂问题时,可以借助调试工具。比较成熟的有Xdebug、ZendDebugger。
以Xdebug为例,它能够:控制打印输出的样式和数组层级、堆栈式的追踪错误、追踪函数调用、代码执行覆盖分析、程序的概要分析(Profiling)、远程调试。详见:http://xdebug.org/docs/。
Xdebug前两个功能是对PHP原有的调试功能做了改进,更方便调试。
复杂的问题,调试不出来的,可能是业务上的的问题,下面也有说业务逻辑的调试。
三、调试业务逻辑错误
当PHP脚本跑起来,没有任何错误,只能说它没有语法上的错误,但是不能说明它业务逻辑上没错误。
很多业务逻辑的错误并不会反映在语法错误上,但调试的思路和PHP自带调试功能差不多。
下面是一些方法。
1、最基本的调试方法
先确定两个东西:程序预期的结果、程序现在的不符合预期的结果;
寻找与两种结果相关的代码片段;
阅读这些代码片段,尝试以“肉眼”找出错误;
找不出,则需要输出一些关键变量,通过检查它们的值是否正确来判断是哪里发生了错误;
若干次尝试,最终你可以确定错误发生在哪个点。
你也可以借助Xdebug等工具,查看变量值的变化,或者设置断点进行调试。
2、记录运行日志
有些复杂或特殊的业务,用上面的方法不合适,比如:一个不能被打断的后台运行脚本。这些情况下记录运行日志比较合适。
记日志的点要有所选择,除了业务上比较重要的点,通常容易出错的地方有:网络连接和通信、系统权限问题等。
3、单元测试
以代码测试代码,而不是像第一点一样调试完会把测试代码丢掉。以测试驱动开发。
这个话题比较大,但适合放这里提一下。有兴趣的同学可以去了解。
四、调试非功能性的错误
非功能性错误,比如:内存溢出导致程序挂掉了、效率有问题导致程序非常慢、死循环了等等。
这些问题,用“肉眼”检查代码效率太低了。
所以可以借助调试工具做程序的概要分析(Profiling),从中检查出程序的瓶颈所在。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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 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.

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.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

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

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

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 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.
