Use print statements, error reports, and phpeclipse plugins.
There are a number of PHP debugging techniques that can save a lot of time when coding. An effective but basic debugging technique is to open a bug report. Another, slightly more advanced technique includes the use of print statements, which help pinpoint harder-to-find bugs by showing what actually appears on the screen. phpeclipse is an eclipse plug-in that highlights common syntax errors and can be used in conjunction with a debugger to set breakpoints.
To learn Xi concepts described in this article, you'll need PHP, a web server, and Eclipse. The version of PHP supported by the debugger extension is v50.3。
We need a web server to parse pages created in PHP and display them to the browser. This article is using apache2. However, any web server will suffice.
To take advantage of some of the debugging techniques described in this article, you need to install Eclipse v31.1 and plugin phpeclipse v11.8。Since Eclipse requires J**A technology, it also needs it.
PHP's debugger extension is also required. It's a little cumbersome to install. Follow carefully the instructions for installing the debugger extension. Now, let's start with phpThe ini file comments out the lines that require the PHP extension to be loaded and configured. Uncomment when you need to use the debugger.
Please refer to the reference resources for ** information. Now for the error message.
Error messages are the first line of defense for developers. No one wants to develop in PHP on a server that isn't configured to display error messages**. However, keep in mind that when debugging is complete and ready to run, you should make sure that error reporting is turned off, as you don't want visitors to your site to see the error message, as this will give them enough information to exploit your site's weaknesses and hack your site.
You can also use error messages to your own service, as they show the correct line that threw or generated the error. This becomes a matter of looking at the line number displayed by the generated error on the browser and checking that line in **. Later on, you'll see that the phpeclipse plugin can be a great help during development and debugging by instantly underlining syntax errors and marking them with a red "x" when saving files.
Let's start by looking at how to do it in phpEnable error reporting in the ini file and set the level of error reporting. You'll then learn how Xi override these settings in your apache configuration file.
php.There are many configuration settings in the ini file. You should already have your php set upini file and put it in the appropriate directory, as shown in the documentation instructions for installing php and apache 2 on linux (see Related topics). When debugging a PHP application, you should be aware of two configuration variables. Here are the two variables and their default values:
display_errors = offerror_reporting = e_all
By running in phpSearching for them in the ini file reveals the current default values for both variables. display_errors
The purpose of the variable is obvious - it tells php whether or not to show an error. The default value isoff
。However, to make the development process easier, set this value toon
display_errors = on
error_reporting
The default value for the variable ise_all
。This setting shows everything from bad coding practices to innocuous prompts to errors. e_all
It's a bit too thin for the development process, as it also shows hints on the screen for small things (e.g. variables are not initialized) that messes up the browser's output. I just want to see bugs and bad coding practices, but I don't want to see harmless prompts. So, use the following values insteaderror_reporting
Default value:
error_reporting = e_all & e_notice
Restart Apache and you're all set. Next, you'll learn how Xi do the same on Apache.
Depending on what Apache is doing, opening a bug report in PHP may not work, as there may be multiple versions of PHP on the computer. Sometimes it's hard to tell which PHP version Apache is using, because Apache can only look at one PHPini file. Don't know which php Apache is usingThe ini file configures itself as a security issue. However, there is a way to configure php variables in Apache to ensure that the correct error level is set.
And it's good to know how to set these config variables server-side to veto or preempt phpini file, thus providing a higher level of security.
When configuring Apache, you should have already touched Conf HTTPDconf in httpBasic configuration in the conf file.
Do it in phpWhat has already been done in the ini file, add the following lines to httpdconf, override any phpini file.
php_flag display_errors onphp_value error_reporting 2039
This overrides phpini filedisplay_errors
flags that have been set as wellerror_reporting
value. value
Representativese_all & e_notice
。If willing to adopte_all
, set the value to
。Again, restart Apache.
Next, you want to test the error report on the server.
If you start an error report, you can save a lot of time. Errors in PHP point to errors in **. Please create a simple php file testphp and define it as shown in Listing 1.
Listing 1A simple php that generates errors
The firstprint()
A statement displays its contents to a web browser. But the second statement generates an error and displays it on the web page. This causes the last oneprint()
The statement doesn't work, as shown in Figure 1.
Figure 1Generate an error.
Bug reporting is now enabled!Next, use the print statement to help debug the application.
Because functional bugs in an application don't produce errors, in all debugging strategies, it's about how to place and use them correctlyprint
ordie
The knowledge of statements to debug php applications is a great asset. Yesprint
Statements narrow down the location of problem statements in **, which are not syntactically error-free and not bugs, but are functionally bug-like. These are the most difficult bugs to find and debug because they don't throw an error. The only thing you know is that what is displayed on the browser is not what you want, or what you want to save in the database is not saved at all.
Let's say it's being processedget
The request was sent to the form data, and I wanted to display the information to the browser, but for some reason, the data was not submitted correctly, or it was not correctly taken fromget
Requested to read out. To debug this kind of problem, it's important to useprint()
ordie()
The statement knows what the value of the variable is.
die()
The statement aborts program execution and displays text on the web browser. If you don't want to comment out **, and you only want to show the information before the error and the error information, and you don't want to show the information after it, thendie()
Statements are particularly useful.
Let's test this concept with a print statement in PHP.
In my days as a programmer, when I was developing an application on Linux, there was no convenient GUI that could tell me where the bug was, and I quickly realized that the more print statements I put in the program, the better my chances of narrowing down the bug to one line in the application. Please create another php file test2php, and define it as shown in Listing 2.
Listing 2Displays all variables submitted via get.
$i)if($_get['submit'] == "send get request") $j = "done!
You may find bugs in Listing 2 very easily!You're great!Note, however, that this is a very simple script, and is just an example of debugging with a print statement. This script is just extractingget
All variables in the request, if any, are displayed on the browser. A form is also provided, with:get
Request to send variables to the server for testing. Take a look at the output, as shown in Figure 2.
Figure 2 test2.PHP output.
Now clicksend get requestbutton, please note only$_get
The requested key is displayed on the browser, but the correct value is not displayed. You can put a print statement in the loop to check inforeach
Whether data actually exists in each element in the loop. See Listing 3.
Listing 3Verify the functionality of ** with a print statement.
...foreach($_get as $key => $i)..
The print statement is bold. Note that it is now known to be displayed on a web browser$key
The value is correct, but for some reason, the value is not displayed correctly. Take a look at the new output, as shown in Figure 3.
Figure 3Modified test2PHP output.
Now it is known that the application is correctly fromget
If the request receives a variable, then there must be a bug in **. After looking at it, you notice that the variable used to display the value$j
is wrong. Inforeach
statement$i
, so it would definitely have the correct value, but was inadvertently entered$j
。So by put$j
Replace with$i
, quickly fixed the error, and when the page was reloaded, I saw the correct output, as shown in Figure 4.
Figure 4Corrected test2PHP output.
Now it's time to delete or comment out the print statement you just added, because the bug in ** has been found. Note that this is only a small subset of the many errors you may encounter when debugging your application. A good solution to problems you may encounter when working with a database is to output SQL statements to ensure that the SQL executed is exactly what you want to execute.
Now let's take a look at how you can use the Eclipse IDE and the PhpeClipse plug-in and debugger extensions to further help in your debugging journey.
You may have used Eclipse, but you may not be familiar with it. See Related topics for an introduction to the Eclipse platform.
The phpeclipse plugin for Eclipse is a popular tool for developing PHP applications. Please launch Eclipse and specify the workspace directory as the Apache www directory (c: www on my machine). Now clickfile > new > project。The new project wizard pops up. Double-click on the PHP folder and select PHP Project. Clicknext, enter the project name DebugArticle, and clickfinish
If you set the web server to listen on port 80, you don't need to make any changes. Otherwise, go to the n**igator window in the PHP projectdebugarticle, select Properties, and then clickphp project settings。Clickconfigure workspace settingsThen modify the appropriate localhost or add a port for the web server to listen on (for examplehttp://localhost:8080
)。ClickapplyComplete the setup.
The n**igator window should display the project and aproject file. Right-click on the item, just like you did earlier, only this time you selectnew > php file。Use the name test3PHP replacement*.php
, and then clickfinish。A new file should appear in the Eclipse IDE. You may need to navigate to the PHP browser at the bottom of the window to see the current output of the PHP file (see Figure 5).
Figure 5PhpeClipse plug-in for Eclipse.
Note that only Windows users can use the PHP browser as shown in Listing 5. The same functionality can be used by opening a separate browser window and pointing the browser to the directory where the test script is located.
Now come and demonstrate this app and prove its great capabilities.
In the "Using the Debugger" section, you'll Xi learn how to debug a PHP application with Eclipse, PhpeClipse, and the debugger PHP extension from the previous **. Let's start by learning how Xi can use its syntax parsing feature.
Let's start by looking at how PhpeClipse can help debug PHP applications' real-time parsing capabilities. To see this feature in action, let's start by defining test3PHP starts, as shown below.
Note that the two characters underlined in Listing 4 are underlined in Eclipse, indicating incorrect syntax. Pressctrl+sSave the file and the parsing error will be displayed in Eclipse: a red "x" will be added to the line corresponding to the parsing error in **, as shown in Figure 6.
Figure 6Grammatical errors are emphasized.
Now demo the php browser. This window provides a preview of the current PHP script, as shown in Figure 6.
From the test3 defined abovephp to remove the comma ()。Pressctrl+sSave the file and watch the PHP browser window update to show Hello World (see Figure 7).
Figure 7Preview PHP scripts in phpeclipse.
Here's how to set breakpoints in PHP with the debugger.
Using the debugger, you can set breakpoints and see the browser output from PHP ** up to the set breakpoint. You can then continue to execute and see the browser output before the next breakpoint, and then to the next until the php script completes.
Now put the "Settings" section in phpThe commented out line in ini is uncommented and Apache is restarted. Now that the debugger is loaded, Eclipse is able to hook up with it.
Now design the debugging environment in Eclipse. Please create a new test4php file, leave it empty for now. Now clickrun > debug。Select PHP DBG Script in the left panel and clicknew。Now go tofiletab, enter the current projectdebugarticleand the files you want to debugtest4.php。Now go toenvironmenttab, and then againinterpreterSubtabs. Find php. in the directory where php is installedexe file (mine is c: apps php5.)0.3\php.exe)。Now clickremote debugsubtab, selectremote debugIf you're not using Windows, clear the "Open with DBGSession URL in Internal Browser Box" checkbox. Set the remote source path to be the same as the absolute path (not the web path) of the php script you want to test (my setting is c: www debugarticle test4.).php)。Now clickdebug
You should now load the debug perspective, as shown in Figure 8. Otherwise, clickwindow > open perspective > otherand selectdebug
Figure 8Debug perspective in Eclipse.
You can now set a breakpoint.
For the versions of plugins and extensions used in this article, the breakpoint feature is necessary because PHP buffers the output before sending it to the browser. Beyond that, you need to do more than just set a breakpoint to flush the current display data to the web browser, so define test4 as shown below and in Figure 8php。
Listing 4Set and create breakpoints.
breakpoint()
The function flushes the buffered output and other buffered data to the web browser. Rightsleep(.1)
The call is required so that the call is aborted indebugbreak()
Previously, the server had enough time to flush the data to the web browser, which was an internal function of the previous PHP debugger extension. In this way, callbreakpoint()
will put html blocks,print()
withecho()
The data for the statement is flushed to the browser, and then the execution is aborted.
After you've written like Listing 4, you can open your browser and point to test4php, or you can check out the php browser window (mine ishttp://localhost/debugarticle/test4.php
)。Every time you enter and save a file, the debugging sequence is already started in the PHP browser window. If you're not using Windows, look at test4 through your browserphp。After having saved the file, usef8or clickrun > resumeProceed to execution. Continue to do this until the last line of output isend!
up to the point (see Figure and 11).
Figure 9Initial PHP browser output to the first breakpoint.
Notice how the debug window in Figure 9 shows execution as pending.
Figure 10PHP browser output after the first breakpoint to before the second breakpoint.
The debug window in Figure 10 still shows the execution as pending, while the second set of data is displayed in the PHP browser.
Figure 11Full php browser output.
Notice that the ** in the debug window in Figure 11 is no longer hanging, and the entire script has been executed, as shown in the php browser in Figure 11.
Now that we've seen the advantages of developing with phpeclipse and debugger extensions, it's hard to imagine it without it.
Now that the use of error reporting, print statements, phpeclipse, and debugger extensions have been added to PHP's debugging technology set, you can become a more effective PHP coder by reducing the number of errors per line. See Related topics for some PHP tutorials on which you can test these new skills.