


Although you can use PHP to create command-line scripts for tasks like system administration and traditional data processing, the programming language has a major impact on the performance of your web application. During use, each PHP application resides on the server, and the PHP application will be called through a proxy (such as Apache) to handle incoming requests. For each request, a typical PHP web application will get a web page or XML data structure after a brief run.
Assuming that after a simple run, a web application constructed in layers - including client, network, HTTP server, application code and underlying database - will be difficult to isolate errors in the PHP code. Even assuming that all layers except the PHP code are functioning properly, tracking down bugs in the PHP code can be very difficult, especially if the application utilizes a large number of classes.
The PHP statement echo and the functions var_dump(), debug_zval_dump() and print_r() are common and popular debugging aids that can help solve a variety of problems. However, these statements - and even more robust tools such as the PEAR Log package - are forensic tools that must first be speculatively analyzed outside of context to generate evidence.
To a certain extent, debugging through inference is a foolhardy approach. Collect and sift through data to try to deduce what went wrong. If important information is missing, you must retest the code, repeat the steps, and start research again. A more efficient approach is to probe the application while it is running. You can sort request parameters, filter the procedure call stack, and query for any desired variables or objects. You can temporarily interrupt your application and receive alerts when variables change value. In some cases, you can actually influence variables by interactively asking "What if?" questions.
Special applications called debuggers support this “live” or interactive inspection. A debugger may be started and attached to the process in order to control the process and monitor its memory. Alternatively, in the case of an interpreted language, the debugger can interpret the code directly. A typical modern graphical debugger can index and browse code, easily display complex data structures in a human-readable form, and simultaneously display program state such as the call stack, intermediate output, and the values of all variables. For example, debuggers typically categorize and describe class properties and methods.
In this and the next article, I will introduce tools that are sure to simplify PHP debugging. Next time, I'll focus on interactive debugging and the Zend Debugger - a robust debugger specifically for PHP - and explore the many features it offers. (Zend Debugger is a commercial product and is part of the Zend PHP integrated development environment (IDE)). I'll also introduce an open source PHP debugger, lest you spend your money on beer instead of code. However, this article will focus on how to better collect evidence.
Similar to CSI, only more annoying
When your code goes wrong, fails to produce a desired result, or crashes completely, you need to answer the four w questions: where, what, why, and when:
“where” is the file and line number where the application last ran normally.
“What” is code for a mistake – for example, a suspect.
“Why” is the essence of the error. Maybe it's a logic error and/or an error caused by interaction with the operating system, or both.
And “when” is the context when an error occurs. What happened before the program was terminated? As in all crimes, if you can gather enough clues, the clues can help you find the culprit.
A forensic tool Xdebug (the tool used in the previous article to analyze PHP application performance), as the name suggests, will provide several functions that illustrate the status of the program, and should be added to the command system A valuable research tool (see Resources). Once installed, Xdebug will prevent infinite recursion (ostensibly), fix error messages about stack traces and function traces, and monitor memory allocations, among other features. Xdebug also includes a set of functions that you can add to your code for runtime error diagnosis.
For example, the following code will test the callee() function using some xdebug_...() steps to output the exact location of the calling program, including the file name, line number, and name of the calling function.
Listing 1. Steps to test the callee() function
Function callee( $a ) {
echo sprintf("callee() called @ %s: %s from %s",
xdebug_call_file(),
xdebug_call_line(),
xdebug_call_function()
);
}
$result = callee( "arg" );
?>
This code will generate:
callee() called @ /var/www/catalog/xd.php: 10 from {main}
Back to top Building and installing Xdebug
Xdebug can be easily built from source code in UNIX®-like operating systems, including Mac OS X. If you are using PHP on Microsoft® Windows®, you can download the latest PHP version of the binary Xdebug module from the Xdebug Web site (see Resources).
Let’s build and install Xdebug for Debian “Sarge” Linux® and PHP V4.3.10-19. As of this writing, the latest version of Xdebug is V2.0.0RC4, released on May 17, 2007. To continue with this article, you must have the phpize and php-config utilities, and you must be able to edit your system's php.ini configuration file (if you don't have the utilities, visit PHP.net for source code and instructions on how to build PHP from scratch). Please follow these steps:
Download the Xdebug tarball (a gzip-compressed .tar archive). The wget command can help you do this easily: $ wget http://www.xdebug.org/files/xdebug-2.0.0RC4.tgz
Extract the tarball and switch to the source code directory: $ tar xzf xdebug-2.0.0RC4.tgz
$ cd xdebug-2.0.0RC4
Run phpize to prepare Xdebug code for your version of PHP: $ phpize
Configuring for:
PHP Api Version: 20020918
Zend Module Api No: 20020429
Zend Extension Api No: 20021010
The output of phpize is a script - usually named configure - that is used to tweak the rest of the build process.
Run the configuration script: $ ./configure
Checking build system type... i686-pc-linux-gnu
Checking host system type... i686-pc-linux-gnu
checking for gcc... gcc
checking for C compiler default output file name... a.out
Checking whether the C compiler works... yes
checking whether we are cross compiling... no
Checking for suffix of executables...
checking for suffix of object files... o
…
checking whether stripping libraries is possible... yes
Appending configuration tag "F77" to libtool
configure: creating ./config.status
config.status: creating config.h
Build the Xdebug extension by running make: $ make
/bin/sh /home/strike/tmp/xdebug-2.0.0RC4/libtool
--mode=compile gcc -I.
-I/home/strike/tmp/xdebug-2.0.0RC4 -DPHP_ATOM_INC
-I/home/strike/tmp/xdebug-2.0.0RC4/include
-I/home/strike/tmp/xdebug-2.0.0RC4/main
-I/home/strike/tmp/xdebug-2.0.0RC4
-I/usr/include/php4 -I/usr/include/php4/main
-I/usr/include/php4/Zend -I/usr/include/php4/TSRM
-DHAVE_CONFIG_H -g -O0 -c
/home/strike/tmp/xdebug-2.0.0RC4/xdebug.c -o
xdebug.lo mkdir .libs
…
Build complete.
(It is safe to ignore warnings about tempnam and tmpnam).
Using make will generate the Xdebug extension xdebug.so.
Install the extension: $ sudo make install
Installing shared extensions: /usr/lib/php4/20020429/
Before continuing, use the mouse to select and copy the directory shown by the previous command. This path is critical for the final step of configuring the extension.
Open the php.ini file in your favorite text editor and add the following code: zend_extension = /usr/lib/php4/20020429/xdebug.so
xdebug.profiler_enable = Off
xdebug.default_enable = On
The first line will load the Xdebug extension; the second line will disable Xdebug's profiler functionality (just for simplicity), and the third line will enable the extended debugging functionality.
To verify that the Xdebug extension is installed and enabled, restart the web server and create a simple one-line PHP application with the code . If you point your browser to the file -- such as http://localhost/phpinfo.php -- and scroll down, you should see something similar to Figure 1.
Figure 1. Verify whether the Xdebug extension is installed and running

NOTE: If you don’t see the Xdebug section in the output of phpinfo(), Xdebug failed to load. The Apache error log lists the reason. Common errors include the wrong path to zend_extension or conflicts with other extensions. For example, if you need to use XCache and Xdebug, be sure to load XCache first. However, since Xdebug is intended for use during development and assumes the path to xdebug.so is correct, you will need to disable the other extensions and try again. You can then re-enable the extension to perform additional tests, such as the effects of caching. The Xdebug site has some other troubleshooting tips.

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

是的,像XDebug这样的调试器会降低PHP服务器的性能。这就是调试器不放置在服务器环境中的原因。它们部署在不同的环境中,以避免不必要的开销。调试消息无法在已处于生产阶段的应用程序中显示。当将调试行为添加到服务器上,调试引擎附加到PHP进程。它开始接收消息以在断点处停止,但这不是必需的行为,因为它会给其他进程带来高性能打击,从而停止PHP解析器。另一方面,当调试器安装后,它们往往会在服务器中打开端口,因为它们不打算在生产环境中使用。在服务器中打开端口就像为黑客打开一扇窥探之门一样糟糕。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

转化方法:1、使用“mb_substr($url,stripos($url,"?")+1)”获取url的参数部分;2、使用“parse_str("参数部分",$arr)”将参数解析到变量中,并传入指定数组中,变量名转为键名,变量值转为键值。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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

Dreamweaver Mac version
Visual web development tools
