Php 简明教程
PHP - Bugs Debugging
PHP 代码中的故障是指程序中导致意外结果或崩溃的错误。在用户这样做之前寻找错误的过程中的系统方法称为调试。本章给出了在 PHP 代码中跟踪错误的一些重要提示。
A bug in a PHP code refers to an error in the program that leads to unexpected results or crash. A systematic approach towards the process of finding bugs before users do is called debugging. In this chapter, some important tips to trace bugs in a PHP code are given.
程序很少在第一次工作时就正确运行。您的程序中会出现很多问题,导致 PHP 解释器生成错误消息。您可以选择这些错误消息的去向。消息可以连同其他程序输出一起发送到网络浏览器。它们还可以包含在“Web 服务器错误日志”中。
Programs rarely work correctly the first time. Many things can go wrong in your program that can cause the PHP interpreter to generate an error message. You have a choice about where those error messages go. The messages can be sent along with other program output to the web browser. They can also be included in the "web server error log".
要使错误消息显示在浏览器中,将“display_errors”配置指令设置为 ON。确保在“php.ini”文件中启用了以下设置。
To make error messages display in the browser, set the "display_errors" configuration directive to ON. Ensure that the following settings are enabled in the "php.ini" file.
display_errors=On
display_startup_errors=On
您还可以使用 ini_set() function 覆盖“pnp.ini”配置 −
You can also use the ini_set() function to override the "pnp.ini" configuration −
ini_set('display_errors', 1)
ini_set('display_startup_errors', 1)
要将错误发送到 Web 服务器错误日志中,请将“log_errors”设置为 ON。如果您同时想要这两个地方都有错误消息,则可以将两者都设置为 On。
To send errors to the web server error log, set "log_errors" to ON. You can set them both to On if you want error messages in both places.
PHP 定义了一些 constants ,可用于设置 error_reporting 的值,以只报告特定类型的错误 −
PHP defines some constants that you can use to set the value of error_reporting such that only errors of certain types get reported −
-
E_ALL (for all errors except strict notices)
-
E_PARSE (parse errors)
-
E_ERROR (fatal errors)
-
E_WARNING (warnings)
-
E_NOTICE (notices)
-
E_STRICT (strict notices)
在编写 PHP 程序时,最好使用 PHP 感知编辑器,例如 BBEdit 或 Emacs。这些编辑器的一个特色功能是语法高亮显示。它会根据程序中不同部分的内容更改这些部分的颜色。例如,字符串为粉色,if 和 while 等关键词为蓝色,注释为灰色,变量为黑色。
While writing your PHP program, it is a good idea to use PHP-aware editors like BBEdit or Emacs. One of the special features of these editors is syntax highlighting. It changes the color of different parts of your program based on what those parts are. For example, strings are pink, keywords such as if and while are blue, comments are grey, and variables are black.
Microsoft 的 VS Code 也是编辑 PHP 代码的不错选择。如果您安装 VS Code 扩展 Intelephense,在编辑器窗口中输入 PHP 语句时,将获得类型提示和错误消息。
VS Code from Microsoft is also a good choice for editing PHP code. If you install VS Code extension Intelephense, you will get type hints and error message as you enter PHP statements in the editor window.
另一个功能是引号和括号匹配,它有助于确保您的引号和括号处于平衡状态。当您键入“}”之类的闭合定界符时,编辑器将高亮显示它匹配的开“{”。
Another feature is quote and bracket matching, which helps to make sure that your quotes and brackets are balanced. When you type a closing delimiter such as "}", the editor highlights the opening "{" that it matches.
Points to Check while Debugging a Code
在调试程序代码时,需要验证以下几点 −
One needs to verfity the following points while debugging a program code −
Missing Semicolons
每条 PHP 语句都以分号 (;) 结束。PHP 不会停止读取语句,直到它到达分号。如果您在行的末尾省略分号,PHP 将继续在下一行读取该语句。
Every PHP statement ends with a semicolon (;). PHP doesn’t stop reading a statement until it reaches a semicolon. If you leave out the semicolon at the end of a line, PHP continues reading the statement on the following line.
Not Enough Equal Signs
当您询问比较语句中的两个值是否相等时,您需要两个等号 (==)。使用一个等号是一个常见的错误。
When you ask whether two values are equal in a comparison statement, you need two equal signs (==). Using one equal sign is a common mistake.
Misspelled Variable Names
如果您拼写错误了一个变量,那么 PHP 将其理解为一个新变量。请记住:对于 PHP 来说,$test 并不是与 $Test 相同的变量。
If you misspelled a variable then PHP understands it as a new variable. Remember: To PHP, $test is not the same variable as $Test.
Missing Dollar Signs
变量名中缺少美元符号确实很难看出来,但至少它通常会导致一条错误消息,以便您知道在哪里查找问题。
A missing dollar sign in a variable name is really hard to see, but at least it usually results in an error message so that you know where to look for the problem.
Troubling Quotes
引号可能太多、太少或类型错误。因此,检查引号是否数量平衡。
You can have too many, too few, or the wrong kind of quotes. So check for a balanced number of quotes.
Array Index
PHP 中的数组是一个项的集合,每个项都分配了一个从 0 开始的增量索引。
An array in PHP is a collection of items, each item assigned an incrementing index starting with 0.
此外,正确处理所有错误并将所有跟踪消息定向到系统日志文件中,以便在发生任何问题时将其记录到系统日志文件中,这样您就可以调试该问题。
Moreover, handle all the errors properly and direct all trace messages into system log file so that if any problem happens then it will be logged into system log file and you will be able to debug that problem.