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:
- // Ini_set ('error _ report', E_ALL ^ E_NOTICE); // display all error messages except the notice type
- Ini_set ('error _ report', E_ALL); // display all error messages
- Ini_set ('display _ errors ', off); // you cannot output error messages to the output end.
- Ini_set ('log _ errors ', On); // enable error logging
- 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:
-
-
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:
-
- $ A = array (1, 2, array ("a", "B", "c "));
- Var_dump ($ );
- $ B = 3.1;
- $ C = TRUE;
- Var_dump ($ B, $ c );
- ?>
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:
-
- $ A = array ('a' => 'apple ',
- 'B' => 'bana ',
- 'C' => array ('X', 'y', 'z '));
- Print_r ($ );
- ?>
The above code will be output:
- 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:
-
- $ B = array ('M' => 'monkey ',
- 'Foo' => 'bar ',
- 'X' => array ('X', 'y', 'z '));
- $ Results = print_r ($ B, true); // $ results contains the output result of print_r
- ?>
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:
- Function ss_array_as_string (& $ array, $ column = 0 ){
- $ Str = "Array (
- N ";
- While (list ($ var, $ val) = each ($ array )){
- For ($ I = 0; $ I <$ column + 1; $ I ++ ){
- $ Str. = "& nbsp ;";
- }
- $ Str. = $ var. ==> ;;
- $ Str. = ss_as_string ($ val, $ column + 1 )."
- N ";
- }
- For ($ I = 0; $ I <$ column; $ I ++ ){
- $ Str. = "& nbsp ;";
- }
- Return $ str .);
- }
- Function ss_object_as_string (& $ object, $ column = 0 ){
- If (emptyempty ($ object->; classname )){
- Return "$ object ";
- }
- Else {
- $ Str = $ object->; classname ."(
- N ";
- While (list (, $ var) = each ($ object->; persistent_slots )){
- For ($ I = 0; $ I <$ column; $ I ++ ){
- $ Str. = "& nbsp ;";
- }
- Global $ var;
- $ Str. = $ var. ==> ;;
- $ Str. = ss_as_string ($ var, column + 1 )."
- N ";
- }
- For ($ I = 0; $ I <$ column; $ I ++ ){
- $ Str. = "& nbsp ;";
- }
- Return $ str .);
- }
- }
- Function ss_as_string (& $ thing, $ column = 0 ){
- If (is_object ($ thing )){
- Return ss_object_as_string ($ thing, $ column );
- }
- Elseif (is_array ($ thing )){
- Return ss_array_as_string ($ thing, $ column );
- }
- Elseif (is_double ($ thing )){
- Return "Double (". $ thing .")";
- }
- Elseif (is_long ($ thing )){
- Return "Long (". $ thing .")";
- }
- Elseif (is_string ($ thing )){
- Return "String (". $ thing .")";
- }
- Else {
- Return "Unknown (". $ thing .")";
- }
- }