Php asynchronous debugging and online debugging of website programs

Source: Internet
Author: User
What should I do when a website needs to run continuously but debugging program errors of the website? Whether to rely on experience for a little bit of speculation, or directly print the error information to make it lose on the page

What should I do when a website needs to run continuously but debugging program errors of the website? Is it based on experience or simply printing error messages for them to output on the page?

Below we will share a debugging method that meets both of these conditions to facilitate website program error debugging without affecting the normal operation of the website. Copy the following php statement to the top of the public code. the instance code is as follows:

  1. // Ini_set ('error _ report', E_ALL ^ E_NOTICE); // display all error messages except the notice type
  2. Ini_set ('error _ report', E_ALL); // display all error messages
  3. Ini_set ('display _ errors ', off); // you cannot output error messages to the output end.
  4. Ini_set ('log _ errors ', On); // enable error logging
  5. Ini_set ('error _ log', 'C:/phpernote'); // defines the location where error logs are stored.
The following code adds two commonly used PHP statements for troubleshooting error messages:

@ Ini_set ('memory _ limit ', '500m'); // sets the maximum memory occupied by the program to 500 MB.

@ Ini_set ('max _ execution_time ', '000000'); // sets the maximum execution time allowed by the program to 180 seconds.

Supplement

Die () and exit () are also a common php debugging method.

Both die () and exit () functions terminate threads and are the most important functions required for php breakpoint debugging. They are also frequently used functions by php programmers. But what are the differences between the two? What should I pay attention to during program debugging?

The die () function is generally used together with "or". when writing "or die ()", we often see the following statements:

$ File = fopen ($ filename, 'r') or die ("Sorry, cannot open: $ filename ")

Or here, it is understood that, because in PHP, the data type is not distinguished, so $ file can be either int or bool, so such a statement will not report an error. However, some friends may not understand the process. In most languages, in statements such as bool or bool, if the previous value is true, the latter value will no longer be judged. So if the fopen function is correctly executed, an int value greater than 0 will be returned (this is actually "true"), and subsequent statements will not be executed. If the fopen function fails to be executed, false is returned. then, the system checks whether the following expression is true. After the die () is executed, the program stops running no matter what is returned, and the specified error message is displayed, thus debugging is achieved. That's it.

In fact, die and exit are equivalent and are used to terminate the current script.

The php manual explains the two as follows:

The exit () function outputs a message and exits the current script. This function is the alias of the die () function.

The die () function outputs a message and exits the current script. This is the alias of the exit () function.

The instance code is as follows:

  1.  
  2.    

Var_dump () and print_r ()

Var_dump -- print information about a variable

Void var_dump (mixed expression [, mixed expression [,...])

This function displays the structure information about one or more expressions, including the expression type and value. The array recursively expands the value and displays its structure through indentation.

Tip: to prevent the program from directly outputting the results to the browser, you can use the output-control function to capture the output of this function, and save them to a variable of the string type. the code is as follows:

  1. $ A = array (1, 2, array ("a", "B", "c "));
  2. Var_dump ($ );
  3. $ B = 3.1;
  4. $ C = TRUE;
  5. Var_dump ($ B, $ c );
  6. ?>

Var_dump () can output multiple variables, such as: var_dump ($ B, $ c)

Print_r -- print easy-to-understand information about variables

Bool print_r (mixed expression [, bool return])

Note: The return parameter is added in PHP 4.3.0.

Print_r () displays easy-to-understand information about a variable. If the value is string, integer, or float, the variable value is printed. If array is provided, keys and elements are displayed in a certain format. Objects are similar to arrays.

Remember, print_r () will move the array pointer to the last edge. Use reset () to bring the pointer back to the beginning. the code is as follows:

  1. $ A = array ('a' => 'apple ',
  2. 'B' => 'bana ',
  3. 'C' => array ('X', 'y', 'z '));
  4. Print_r ($ );
  5. ?>

The above code will be output:

  1. Array ([a] => apple [B] => banana [c] => Array ([0] => x [1] => y [2] => z))

If you want to capture the output of print_r (), you can use the return parameter. If this parameter is set to TRUE, print_r () will not print the result (this is the default action), but return its output.

Example: return parameter example. the code is as follows:

  1. $ B = array ('M' => 'monkey ',
  2. 'Foo' => 'bar ',
  3. 'X' => array ('X', 'y', 'z '));
  4. $ Results = print_r ($ B, true); // $ results contains the output result of print_r
  5. ?>

Note: If you want to capture the output of print_r () in a version earlier than PHP 4.3.0, you can use the output control function.

Note: In versions earlier than PHP 4.0.4, if the given array or object contains a reference pointing directly or indirectly to itself, print_r () will continue forever. Print_r ($ GLOBALS) is an example, because $ GLOBALS itself is a global variable and contains references pointing to itself.

The following functions allow you to view the types and values of any variables in the program at any time. the code is as follows:

  1. Function ss_array_as_string (& $ array, $ column = 0 ){
  2. $ Str = "Array (
  3. N ";
  4. While (list ($ var, $ val) = each ($ array )){
  5. For ($ I = 0; $ I <$ column + 1; $ I ++ ){
  6. $ Str. = "& nbsp ;";
  7. }
  8. $ Str. = $ var. ==> ;;
  9. $ Str. = ss_as_string ($ val, $ column + 1 )."
  10. N ";
  11. }
  12. For ($ I = 0; $ I <$ column; $ I ++ ){
  13. $ Str. = "& nbsp ;";
  14. }
  15. Return $ str .);
  16. }
  17. Function ss_object_as_string (& $ object, $ column = 0 ){
  18. If (emptyempty ($ object->; classname )){
  19. Return "$ object ";
  20. }
  21. Else {
  22. $ Str = $ object->; classname ."(
  23. N ";
  24. While (list (, $ var) = each ($ object->; persistent_slots )){
  25. For ($ I = 0; $ I <$ column; $ I ++ ){
  26. $ Str. = "& nbsp ;";
  27. }
  28. Global $ var;
  29. $ Str. = $ var. ==> ;;
  30. $ Str. = ss_as_string ($ var, column + 1 )."
  31. N ";
  32. }
  33. For ($ I = 0; $ I <$ column; $ I ++ ){
  34. $ Str. = "& nbsp ;";
  35. }
  36. Return $ str .);
  37. }
  38. }
  39. Function ss_as_string (& $ thing, $ column = 0 ){
  40. If (is_object ($ thing )){
  41. Return ss_object_as_string ($ thing, $ column );
  42. }
  43. Elseif (is_array ($ thing )){
  44. Return ss_array_as_string ($ thing, $ column );
  45. }
  46. Elseif (is_double ($ thing )){
  47. Return "Double (". $ thing .")";
  48. }
  49. Elseif (is_long ($ thing )){
  50. Return "Long (". $ thing .")";
  51. }
  52. Elseif (is_string ($ thing )){
  53. Return "String (". $ thing .")";
  54. }
  55. Else {
  56. Return "Unknown (". $ thing .")";
  57. }
  58. }

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.