PHP basic knowledge learning

Install the PHPEclipse plugin under Eclipse3.4

Eclipse plug-in installation address: http://phpeclipse.sourceforge.net/update/stable/1.2.x/

 

The installation steps help/softwareupdates/available Software/add site under eclipse write the plug-in address and I won’t say much about it. The following article was found on the Internet, and it feels good, but it is complicated for people who are starting to learn php with me.

转自IBM Developerworks:http://www.ibm.com/developerworks/cn/opensource/os-debug/

 

Using print statements, error reporting, and the PHPEclipse plugin

This article describes various ways to debug PHP applications, including turning on error reporting in Apache and PHP, and finding the source of more difficult bugs by placing strategically placed print statements in a simple PHP script. It also introduces the PHPEclipse plug-in for Eclipse, a flexible development environment with real-time parsing capabilities, and the DBG debugger extension for PHPEclipse.

 

 

Introduction

There are many PHP debugging techniques that can save a lot of time while coding. An effective but basic debugging technique is to turn on error reporting. Another slightly more advanced technique involves using print statements, which can 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.

 

 

set up

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

We need a web server to parse pages created in PHP and display them to the browser. Apache2 is used in this article. However, any web server will suffice.

To take advantage of some of the debugging techniques described in this article, you need to install Eclipse V3.1.1 and the plug-in PHPEclipse V1.1.8. Since Eclipse requires Java™ technology, it is also downloaded.

The debugger extension for PHP is also required. It's a little tricky to install. Follow the instructions for installing the debugger extension carefully. Now, start by commenting out the lines in the php.ini file that require PHP extensions to be loaded and configured. Uncomment it when you need to use the debugger.

See  Resources  for download information. Now for the error messages.

 

error message

Error messages are your first line of defense as a developer. Nobody wants to develop code in PHP on a server that isn't configured to display error messages. However, keep in mind that when the code is debugged and ready to run, you should make sure that error reporting is turned off, as you don't want your site visitors to see error messages, as this will give them enough information to exploit the site's weaknesses and Hack the site.

Also serve yourself with error messages, as they will show you the correct line of code that threw or generated the error. In this way, debugging becomes looking at the line number displayed by the generated error on the browser, and inspecting this line in the code. As you'll see later, the PHPEclipse plug-in can be of great help during development and debugging by underlining syntax errors on the fly and marking them with a red "x" when saving the file.

Let's first look at how to enable error reporting and set the level of error reporting in the php.ini file. Then learn how to override these settings in Apache's configuration files.

Error reporting for PHP

There are many configuration settings in the php.ini file. You should already have your php.ini file set up and in the appropriate directory, as shown in the documentation for installing PHP and Apache 2 on Linux (see Resources  ) . When debugging a PHP application, there are two configuration variables you should be aware of. Here are the two variables and their default values:

 

PHP code 
  1. display_errors = Off  
  2. error_reporting = E_ALL  

 

 The current default values ​​for these two variables can be found by searching for them in the php.ini file. display_errors The purpose of the variable is obvious -- it tells PHP whether to display errors or not. The default value is  Off. However, to make the development process easier, set this value to On:

 

PHP code 
  1. display_errors = On  
  error_reporting  The default value of the variable is  E_ALL . This setting will show everything from bad coding practices to innocuous hints to errors. E_ALL  It's a bit too granular for the development process, as it also shows hints on the screen for trivial things (such as uninitialized variables), which mess up the browser's output. I just want to see bugs and bad coding practices, but not innocuous hints. So, please replace  error_reporting  the default value with the following value:
error_reporting = E_ALL & ~E_NOTICE

Restart Apache and you're all set. Next, learn how to do the same on Apache.

Error reporting on the server

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

Also, it would be good to know how to set these configuration variables on the server side to overrule or preempt the php.ini file, providing an even higher level of security.

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

To do what is already done in the php.ini file, add the following lines to httpd.conf, overriding any php.ini files:

php_flag  display_errors        on
php_value error_reporting       2039

display_errors This overrides the flags already set  in the php.ini file  , as well as error_reporting the values. value  2039 represents  E_ALL & ~E_NOTICE. E_ALLSet the value  to  2047. Again, you still have to restart Apache.

Next, you'll test error reporting on the server.

 

test bug report

A lot of time can be saved if bug reporting is enabled. 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 that generates errors
<?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 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  print() statement to have no effect, as shown in Figure 1.

Figure 1. Build errors

Bug reporting is now enabled! Next, print statements are used to help debug the application.

 

Introducing the print statement

print Because functional bugs in an application do not generate errors, knowledge of how to properly place and use the OR  die statement to debug a PHP application is a great asset in all debugging strategies  . You can use  print sentences to narrow down the location of the problem statement in the code. These statements have no grammatical errors or bugs, but they are bugs in terms of the function of the code. These are the hardest bugs to find and debug because they don't throw errors. 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 you're processing form data sent via  a request, and want to display information to the browser, but for some reason, the data isn't being submitted correctly, or isn't  being read GET correctly from the  request. GETTo debug this type of problem, it is important to   know what the value of the variable is using the print() OR  statement.die()

die() statement halts program execution and displays text on the web browser. die() The statement is especially useful if you don't want to comment out the code, and you only want to display the information before the error and the error message, and you don't want to display the information after it  .

Let's test this concept with a print statement in PHP

Debugging with print statements

In my days as a programmer, when I was developing applications on Linux®, and there was no convenient GUI to tell me where the bugs were, I quickly discovered that the more print statements I put in my program, the worse I got in the application. The better the chances of narrowing the bug down to one line in the . Please create another PHP file test2.php and define it as shown in Listing 2.


Listing 2. Displaying all variables submitted via GET
<?php
 $j = "";
 print("Lets retrieve all the variables submitted to this ");
 print("script via a GET request:<br>");
 foreach($_GET as $key => $i){
     print("$key=$j<br>");
 }
 if($_GET['Submit'] == "Send GET Request")
     $j = "done!<br>";
?>
<form method="GET">
     Name: <input name="name"><br>
     Email: <input name="email" size="25"><br>
     <input name="Submit" type="submit" value="Send GET Request">
</form>

You might spot the bug in Listing 2 very easily! You are great! But note that this is a very simple script, just shown as an example of debugging using print statements. This script simply extracts  GET all the variables in the request and displays them on the browser if there are any. A form is also provided to  GET send variables to the server with requests for testing purposes. Look at the output, shown in Figure 2.


Figure 2. Output of test2.php


 Now click on  the Send GET Request  button and notice that only  $_GET the requested key is displayed on the browser and the correct value is not. You can put a print statement in the loop to verify that  foreach data actually exists in each element in the loop. See Listing 3.


Listing 3. Verifying code functionality with print statements
...
 foreach($_GET as $key => $i){
     print("Correct data? " . $_GET[$key] . "<br>");
     print("$key=$j<br>");
 }
...

The print statement put in is bold. Note that the values ​​displayed on the web browser are now known to  $key be correct, but for some reason the values ​​are not displayed correctly. See the new output, shown in Figure 3.


Figure 3. The output of the modified test2.php



 Now that you know that the application is correctly  GET receiving the variable from the request, there must be a bug in the code. Noticed after reviewing that the variable used to display the value  $j is wrong. foreach is specified in the statement  $iso it will definitely have the correct value, but it was entered inadvertently  $j. So by  $j replacing with  $i, I quickly fixed the error, and after reloading the page, I saw the correct output, as shown in Figure 4.


Figure 4. Corrected output of test2.php

 

Now you can delete or comment out the print statement you just added, since the bug in the code 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 the problems you may encounter when working with databases is to output SQL statements to ensure that the SQL you execute is exactly what you want to execute.

Now it's time to look at how to use the Eclipse IDE and the PHPEclipse plug-in and debugger extensions to further assist in the debugging process.



Using PHP Eclipse

You may have used Eclipse, but you may not be familiar with it. See  Resources  for an introduction to the Eclipse platform.

The PHPEclipse plug-in for Eclipse is a popular tool for developing PHP applications. Please start Eclipse and specify the workspace directory as Apache's www directory (c:/www on my machine). Now click on  File > New > Project . The New Project wizard will pop up. Double-click the PHP folder and select PHP Project. Click  Next , enter the project name debugArticle, and click  Finish .

If you set up your web server to listen on port 80, you don't need to make any changes. Otherwise, go to the Navigator window, right-click on the PHP project  debugArticle  , select Properties, and click  PHP Project Settings . Click  Configure Workspace Settings  and modify the appropriate localhost or add the port the web server listens on (eg http://localhost:8080). Click  Apply  to complete the setup.

The Navigator window should display the project and a .project file. Right-click on the project, as you did before, only this time choose  New > PHP File . Replace *.php with the name of the PHP file you want to create, test3.php, and click  Finish . 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 5. The PHPEclipse plugin for Eclipse

 



 Note that only Windows® users can use the PHP browser as shown in Listing 5. The same functionality can also be used by opening a separate browser window and pointing the browser to the directory where the test script is located.

 

Now let's demo the app and prove its power.

In the "Using the Debugger" section, you'll learn how to debug your PHP application with Eclipse, PHPEclipse, and the debugger PHP extension you downloaded earlier. Start by learning how to use its syntax parsing capabilities.

Syntax parsing and underlining

Start by looking at how PHPEclipse provides real-time parsing capabilities to help debug PHP applications. To see this feature in action, start by defining test3.php in Eclipse, as shown below.

<?php
print(,"Hello World!");
?>

Note that the two characters underlined in Listing 4 are underlined in Eclipse to indicate incorrect syntax. Press  Ctrl+S  to save the file, and a parsing error will be displayed in Eclipse: a red "x" will be added to the line corresponding to the parsing error in the code, as shown in Figure 6.

Figure 6. Grammar error emphasis


 Now demo the PHP browser. This window provides a preview of the current PHP script, as shown in Figure 6.

Remove the comma ( ) from test3.php defined above ,. Press  Ctrl+S  to save the file, and watch the PHP browser window update to display Hello World (see Figure 7).

 


Figure 7. Previewing PHP scripts in PHPEclipse



 Here's how to set a breakpoint in PHP with the debugger.

 

use debugger

Using the debugger, you can set breakpoints and view the browser output from the PHP code up to the set breakpoint. You can then continue code execution and view the browser output up to the next breakpoint, and then the next, until the PHP script completes.

Now uncomment the line in php.ini that was commented out in the "Settings" section and restart Apache. The debugger is now loaded and Eclipse can hook into it.

Now design the debugging environment in Eclipse. Please create a new test4.php file, keep it empty for now. Now click  Run > Debug . Select PHP DBG Script in the left panel and click  New . Now go to  the File  tab and enter the current project debugArticle and the file test4.php  you want to debug  . Now go to  the Environment  tab and then to  the Interpreter  sub-tab. Locate the php.exe file in the PHP installation directory (mine is c:/apps/php5.0.3/php.exe). Now click on  the Remote Debug  sub-tab, select  Remote Debug , and uncheck the "Open with DBGSession URL in internal browser box" checkbox if you are not using Windows. Set the Remote Source path to be the same as the absolute path (not the web path) of the PHP script to be tested (mine is c:/www/debugArticle/test4.php). Now click  Debug .

The Debug perspective should now load, as shown in Figure 8. Otherwise, click  Window > Open Perspective > Other and select  Debug .

 


Figure 8. Debug perspective in Eclipse



 Breakpoints can now be set.

 

For the version of the plug-in and extension used in this article, the breakpoint feature is required because PHP buffers the output before sending it to the browser. Besides that, you need to do more than just set a breakpoint to flush the current display data to the web browser, so define test4.php as shown below and in Figure 8.


Listing 4. Setting and creating breakpoints
<?php
function break-point(){
    ob_flush();
    flush();
    sleep(.1);
    debugBreak();
}
print("This will get shown first, ");
print("as will this<br>");
breakpoint();
print("This won't get shown until after ");
print("continuing the break-point<br>");
breakpoint();
print("END!");
?

breakpoint() 函数会把缓冲的输出和其他缓冲的数据刷新到 Web 浏览器。对 sleep(.1) 的调用是必需的,这样代码中止于 debugBreak() 之前,服务器才有足够的时间把数据刷新到 Web 浏览器,这个函数是前面下载的 PHP 调试器扩展的内部函数。这样,调用 breakpoint() 会把 HTML 块、print() 和 echo() 语句的数据刷新到浏览器,然后中止代码执行。

在像清单 4 那样编写完代码之后,可以打开浏览器并指向 test4.php,或者可以查看 PHP 浏览器窗口(我的是 http://localhost/debugArticle/test4.php)。每次输入和保存文件时,在 PHP 浏览器窗口中就已经启动了调试序列。如果不使用 Windows,请通过浏览器查看 test4.php。在保存了文件之后,用 F8 或单击 Run > Resume 继续代码执行。持续这么做,直到最后一行输出是 END! 为止(参见图 9、10 和 11)。

 


图 9. 初始的到第一个断点的 PHP 浏览器输出



 请注意图 9 中的 Debug 窗口如何把执行显示为挂起的。


图 10. 第一个断点之后到第二个断点之前的 PHP 浏览器输出



 图 10 的 Debug 窗口仍然把执行显示为挂起,而第二组数据显示在 PHP 浏览器中。


图 11. 完整的 PHP 浏览器输出



 
 注意,图 11 的 Debug 窗口中的代码不再挂起,整个脚本已经执行,如图 11 中的 PHP 浏览器所示。

 

既然已经看到了用 PHPEclipse 和调试器扩展进行开发的优势,那么很难想像没有它会怎么样。



结束语

Now that the use of error reporting, print statements, PHPEclipse, and debugger extensions have been added to PHP's collection of debugging techniques, you can become a more effective PHP coder by reducing the number of errors per line of code. See  Resources  for some PHP tutorials on which to test these new skills.

Guess you like

Origin blog.csdn.net/s_ongfei/article/details/5948548