PHP asynchronous debugging and online debugging Web site program

Source: Internet
Author: User
Tags bool mixed

Share a method to meet these two conditions at the same time, not only to facilitate the Web site program error debugging, but also does not affect the normal operation of the site debugging methods. Copy the following PHP statement to the top of the public code.

The code is as follows

Ini_set (' error_reporting ', E_all ^ e_notice);//Show All except the NOTICE type error message
Ini_set (' error_reporting ', e_all)//Display all error messages
Ini_set (' display_errors ', off);/disable error message output to output end
Ini_set (' Log_errors ', ON)//Open error logging
Ini_set (' error_log ', ' c:/phpernote ');//define error log storage location

Add two more common PHP statements that exclude error messages:

The code is as follows

@ini_set (' Memory_limit ', ' 500M ');//Setup can occupy maximum memory of 500MB
@ini_set (' max_execution_time ', ' 180 ');/set allows the maximum execution time of the program to 180 seconds

Add

Die () and exit () are also our common way of debugging PHP

The die () and exit () functions have the function of terminating threads, the most important functions that PHP breakpoints need to use, and they are also used very frequently by PHP programmers. But what's the difference between the two? What are the issues to be aware of when debugging a program?

The die () function is generally used in conjunction with "or", writing "or Die ()" and often sees such statements:

The code is as follows

$file = fopen ($filename, ' r ') or Die ("Sorry, cannot open: $filename")

Or here is the understanding, because in PHP does not distinguish between the data type, so $file can be int also can bool, so such statements do not complain. But the process may not be clear to some friends. In most languages, in a statement like BOOL or bool, a value is not judged if the previous value is true. So is this, so if the fopen function executes correctly, it returns an int value greater than 0 (which is actually "true"), and the following statement is not executed. If the fopen function fails, it returns false, and the subsequent expression is judged to be true. After the result executes die (), the program stops executing regardless of what is returned, and displays the specified error message, which is the purpose of debugging. That's it.

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

The PHP manual explains the two:

The exit () function prints a message and exits the current script. This function is an alias for the Die () function.

The Die () function prints a message and exits the current script. This function is an alias for the exit () function.

Instance:

The code is as follows
<?php $site = "http://www.111cn.net/"; fopen ($site, "R") or exit ("Unable to connect to $site");?>
<?php $site = "http://www.111cn.net/"; fopen ($site, "R") or Die ("Unable to connect to $site");?>

Var_dump () and Print_r ()

Var_dump--Information on printing variables

void Var_dump (mixed expression [, mixed expression [, ...]])

This function displays information about the structure of one or more expressions, including the type and value of the expression. The array recursively expands the value and displays its structure by indenting it.

Tip: To prevent programs from outputting results directly to the browser, you can use the Output control function (Output-control functions) to capture the output of this function and save them to a variable, such as a string type.

The code is as follows
<?php
$a = Array (1, 2, Array ("A", "B", "C"));
Var_dump ($a);
$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: Parameter return is added during PHP 4.3.0

Print_r () displays information that is easy to understand about a variable. If a string, integer, or float is given, the variable value itself is printed. If you give an array, the keys and elements will be displayed in a certain format. object is similar to an array.

Remember, Print_r () moves the pointer to the last edge of the array. Use Reset () to get the pointer back to the beginning.

The code is as follows

<pre>
<?php
$a = Array (' A ' => ' apple '),
' B ' => ' banana ',
' C ' => array (' x ', ' y ', ' z ');
Print_r ($a);
?>
</pre>

The code above will output:

<pre> Array ([a] => Apple [b] => Banana [C] => Array ([0] => x [1] => y [2] => z)) </pre> ;

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 () The result is not printed (this is the default action), but the output is returned.

Example: Return parameter example

The code is as follows

<?php
$b = Array (' m ' => ' monkey '),
' foo ' => ' Bar ',
' X ' => array (' x ', ' y ', ' z ');
$results = Print_r ($b, true); $results contains the output of the Print_r
?>

Note: If you want to capture the output of Print_r () in the previous version of PHP 4.3.0, you can use the output control function.

Note: In the previous version of PHP 4.0.4, if the given array or object contains references that directly or indirectly point to itself, Print_r () will continue forever. Print_r ($GLOBALS) is an example, because the $GLOBALS itself is a global variable that contains a reference to itself.

The following functions allow you to view the type and value of any variable in the program at any time.

  code as follows &NBSP;
function ss_array_as_string (& $array, $column = 0) { 
$str = "Arra Y ( 
N;  
while ($var, $val) = each ($array)) { 
for ($i = 0; $i < $column +1; $i + +) { < br> $str. = "    "; &NBSP

$str. = $var. ==>;  
$str. = ss_as_string ($val, $column + 1). &NBSP
N;  

for ($i = 0; $i < $column; $i + +) { 
$str. = "    "; &NBSP

return $str.); &NBSP

Function ss_object_as_string (& $object, $column = 0) { 
if empty ($object->;classname ) { 
return "$object;  

else { 
$str = $object->;classname." ( 
N;  
while (the list (, $var) = each ($object->;p ersistent_slots)) { 
for ($i = 0; $i < $coLumn; $i + +) { 
$str. = "    "; &NBSP

Global $ $var  
$str. = $var. ==>;  
$str. = ss_as_string ($ $var, column+1). " &NBSP
N;  

for ($i = 0; $i < $column; $i + +) { 
$str. = "    "; &NBSP

return $str.); &NBSP


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. ")"; &NBSP

ElseIf (Is_long ($thing)) { 
return Long (". $thing."); &NBSP

ElseIf (is_string ($thing)) { 
return String (". $thing."); &NBSP

else { 
return Unknown (". $thing."); &NBSP

}

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.