Debugging technology for PHP programmers

Source: Internet
Author: User
Tags configuration settings php debugger php error

Introduction

Many PHP debugging technologies can save a lot of time in coding. An effective but Basic Debugging Technique is to open an error report. Another slightly more advanced technique involves the use of print statements to help precisely identify more difficult bugs by actually appearing on the screen. PHPEclipse is an Eclipse plug-in that can emphasize common syntax errors and can be used with the debugger to set breakpoints.

To learn the concepts described in this article, PHP, Web server, and Eclipse are required. The PHP version supported by the debugger extension is V5.0.3.

We need a Web server to parse the pages created with PHP and display them to the browser. Apache2 is used in this article. However, any Web server can meet the requirements.

To use some Debugging techniques described in this article, you need to install Eclipse V3.1.1 and PHPEclipse V1.1.8. Because Eclipse requires Java™Technology, so you need to download it.

You also need the PHP debugger extension module. Installing it is a little troublesome. Follow the instructions for installing the debugger extension carefully. Now, comment out the lines that require loading and configuring php extensions in the PHP. ini file. Cancel the comment when you need to use the debugger.

Error Message

Error messages are the first line of defense for developers. No one wants to develop code with PHP on a server that is not configured to Display error messages. However, remember that when the Code debugging is complete and you are ready to run it, make sure that the error report is disabled because you do not want the site visitors to see the error message, this will give them enough information to take advantage of the site's vulnerabilities and hack the site.

You can also use error messages to serve yourself, because they will display the correct code lines that throw or generate errors. In this way, debugging becomes to view the row number of the generated error in the browser, and check this line in the code. Later, you will see the PHPEclipse plug-in instantly underline the syntax error and mark the syntax error with the red "x" when saving the file, it can be of great help in the development and debugging process.

First, let's take a look at how to enable the Error Report in the php. ini file and set the error report level. Then we will learn how to overwrite these settings in the Apache configuration file.

PHP Error Report

The php. ini file contains many configuration settings. You should have set your php. ini file and place it in the appropriate directory, as shown in instructions for installing PHP and Apache 2 on Linux (see references ). When debugging a PHP application, you should know two configuration variables. The two variables and their default values are as follows:

Display_errors = Off
Error_reporting = E_ALL
 
By searching for these variables in the php. ini file, you can find the current default values of these two variables. The purpose of the display_errors variable is obvious-It tells PHP whether an error is displayed. The default value is Off. However, to make the development process easier, set this value to On:

Display_errors = On
 
The default value of error_reporting variable is E_ALL. This setting displays all information from poor coding practices to harmless prompts to errors. E_ALL is a little too detailed for the development process, because it displays a prompt on the screen for some trivial matters (for example, the variable is not initialized), it will mess up the browser output. I only want to see errors and bad code practices, but do not want to see harmless prompts. Therefore, replace the default value of error_reporting with the following values:

Error_reporting = E_ALL &~ E_NOTICE
 
Restart Apache and set all the settings. Next, we will learn how to do the same thing on Apache.

Server Error Report

Depending on what Apache is doing, opening an error report in PHP may not work, because there may be multiple PHP versions on the computer. Sometimes it is difficult to tell which PHP version Apache is using, because Apache can only view one php. ini file. I don't know which php. ini file Apache is using to configure itself as a security issue. However, there is a way to configure the PHP variable in Apache to ensure that the correct error level is set.

In addition, it is better to know how to set these configuration variables on the server side to reject or preemptible the php. ini file to provide higher-level security.

When configuring Apache, you should have been exposed to the basic configuration in the http. conf file in <apache2-install-dir>/conf/httpd. conf.

To add the following lines to the httpd. conf file to overwrite any php. ini file:

Php_flag display_errors on
Php_value error_reporting 2039

This overwrites the flag set for display_errors and the value of error_reporting in the php. ini file. The value 2039 represents E_ALL &~ E_NOTICE. If you want to use E_ALL, set the value to 2047. Similarly, restart Apache.

Next, test the Error Report on the server.

Test Error Report

If an error report is started, it will save a lot of time. Errors in PHP point to errors in the code. Create a simple PHP file test. php and define it as shown in Listing 1.
Listing 1. A simple PHP with incorrect generation

<? Php
Print ("The next line generates an error. <br> ");
Printaline ("PLEASE? ");
Print ("This will not be displayed due to the above error .");
?>
The first print () Statement will display its content to the Web browser. However, the second statement will generate an error and display it on the Web page. This makes the last print () Statement ineffective, as shown in 1.


The error report is now enabled! Next, use the print statement to debug the application.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.