Optimized debugging techniques and techniques for PHP programmers

Source: Internet
Author: User
Tags configuration settings ini variables php and php script advantage
Program | programmer | tips | Optimization This article describes various ways to debug your PHP application, including opening error reports in Apache and PHP, and finding the source of more difficult bugs by placing a strategic print statement in a simple PHP script. It also introduces the PHPEclipse plug-in for Eclipse, a flexible development environment with real-time parsing capabilities and an introduction to PHPEclipse's DBG debugger extensions.

   Brief Introduction

There are many PHP debugging techniques that can save a lot of time when coding. An effective but very basic debugging technique is to turn on error reporting. Another slightly more advanced technique involves using print statements to help pinpoint bugs that are more difficult to find by displaying what actually appears on the screen. PHPEclipse is an Eclipse plug-in that can emphasize common syntax errors that can be combined with the debugger to set breakpoints.

   Set up

To learn the concepts described in this article, you need PHP, WEB servers, and Eclipse. The PHP version supported by the debugger extension is V5.0.3.

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

To take advantage of some of the debugging techniques described in this article, you need to install the Eclipse V3.1.1 and plug-in PHPEclipse V1.1.8. Because Eclipse requires Java? Technology, so you have to download it.

You also need a debugger extension module for PHP. It's a little bit cumbersome to install it. Follow the instructions for installing the debugger extensions carefully. Now, first comment out the rows in the php.ini file that require loading and configuring the PHP extension. When you need to use the debugger, uncomment it again.

See Resources for download information. Now let's introduce the error message.

   error Message

An error message is the first line of defense for developers. No one wants to use PHP to develop code on a server that is not configured to display error messages. However, keep in mind that when code debugging is complete and ready to run, you should make sure that error reporting is turned off because you do not want the site's visitors to see the error message because it gives them enough information to take advantage of the site's weaknesses and hack the site down.

You can also use error messages to serve yourself because they display the correct line of code that throws or generates errors. In this way, debugging becomes the line number that is displayed on the browser to see the generated error, and the line is checked in code. Later on, you'll see that the PHPEclipse plug-in can provide great help during development and debugging by underlining syntax errors in an instant and using a red "x" notation for syntax errors when saving files.

Let's look at how to turn on Error reporting in the php.ini file and set the level of error reporting. You will then learn how to override these settings in the Apache configuration file.

   Error Reporting for PHP

There are many configuration settings in the php.ini file. You should have set up your own php.ini file and put it in the appropriate directory, as shown in the documentation for PHP and Apache 2 on Linux (see Resources). When debugging a PHP application, you should know two configuration variables. The following are the two variables and their default values:


By searching for them in the php.ini file, you can find the current default values for these two variables. The purpose of the display_errors variable is obvious-it tells PHP if it shows an error. The default value is off. However, to make the development process easier, set this value to on:


The default value for the error_reporting variable is e_all. This setting displays all information from bad coding practices to harmless hints to errors. E_all is a bit too thin for the development process because it displays hints on the screen for trivial things, such as variable initialization, and can mess up the browser's output. I just want to see errors and bad coding practices, but don't want to see harmless hints. Therefore, replace the default value of error_reporting with the following values:


Restart Apache, and it's all set up. Next, you'll learn how to do the same thing on Apache.

   Error Reporting on the server

Depending on what Apache is doing, opening the error report in PHP may not work because there may be multiple versions of PHP on the computer. Sometimes it's hard to tell which PHP version the Apache is using, because Apache can only view one php.ini file. It is a security issue not knowing which php.ini file Apache is using to configure itself. However, there is a way to configure PHP variables in Apache to ensure that the correct error level is set.

Also, it's best to know how to set these configuration variables on the server side to veto or preempt php.ini files, providing a higher level of security.

When you configure Apache, you should have contacted the basic configuration in the http.conf file in <apache2-install-dir>/conf/httpd.conf.

To do what you've done in the php.ini file, add the following lines to httpd.conf, overwriting any php.ini files:

Php_flag  display_errors on        php_value error_reporting       


This overrides the flag already set for display_errors in the php.ini file, and the error_reporting value. The value 2039 represents E_all & ~e_notice. If you prefer to use E_all, set the value to 2047. Again, restart Apache.

Next, you will test the error report on the server.

[1] [2] [3] [4] Next page



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.