When needed, simply add the following code to the program to view the types and values of the variables (including arrays and objects) used in the program:
echo ss_as_string($my_variable);
Using the following statement, we can directly view the values of all variables in the program:
echo ss_as_string($GLOBALS);
3. Functions that control Log information
Another important way to debug PHP programs is to view the Log information. If the level of Log information and the display content of Log information can be easily controlled, it will bring more convenience to program debugging. The following functions can easily implement this function.
$ss_log_level = 0;
$ss_log_filename = /tmp/ss-log;
$ss_log_levels = array(
NONE => 0,
ERROR => 1,
INFO => 2,
DEBUG => 3);
function ss_log_set_level ($level = ERROR) {
global $ss_log_level;
$ss_log_level = $level;
}
function ss_log ($level, $message) {
global $ss_log_level, $ss-log-filename;
if ($ss_log_levels[$ss_log_level] < $ss_log_levels[$level]) {
// Log information is not displayed
return false;
}
$fd = fopen($ss_log_filename, "a+");
fputs($fd, $level. - [.ss_timestamp_pretty().] - .$message."n");
fclose($fd);
return true;
}
function ss_log_reset () {
global $ss_log_filename;
@unlink($ss_log_filename);
}
In the above function, there are four Log-level variables. When running a PHP program, the Log information can be recorded and displayed only when the level of the Log is lower than the preset level value. For example, add the following statement to the program:
ss_log_set_level(INFO);
Then, when running the PHP program, only ERROR and INFO level LOG information can be recorded and displayed, while DEBUG level information is ignored. In addition, we can also set the displayed information content, and the statements are as follows:
ss_log(ERROR, "testing level ERROR");
ss_log(INFO, "testing level INFO");
ss_log(DEBUG, "testing level DEBUG");
You can also use the following statement to clear the LOG information at any time:
ss_log_reset();
4. Speed test function
To optimize the code, we need a method that can test the code runtime to select the optimal code. The following function can test the time it takes to run the code:
function ss_timing_start ($name = default) {
global $ss_timing_start_times;
$ss_timing_start_times[$name] = explode( , microtime());
}
function ss_timing_stop ($name = default) {
global $ss_timing_stop_times;
$ss_timing_stop_times[$name] = explode(, microtime());
}
function ss_timing_current ($name = default) {
global $ss_timing_start_times, $ss_timing_stop_times;
if (!isset($ss_timing_start_times[$name])) {
return 0;
}
if (!isset($ss_timing_stop_times[$name])) {
$stop_time = explode(, microtime());
}
else {
$stop_time = $ss_timing_stop_times[$name];
}
$current = $stop_time[1] - $ss_timing_start_times[$name][1];
$current += $stop_time[0] - $ss_timing_start_times[$name][0];
return $current;
}
Now it is easy to check the execution time of any piece of code. We can even use multiple timers at the same time, just set different parameters as the timer name when using the above functions.
5. Debugging and optimizing database operations
For databases, running speed is crucial. Although many books and articles teach some methods to run databases quickly, all methods must be tested by practice. Below we will combine the query() function in the PHPLib function library with the several functions introduced above to write it into a new query() function. Compared with the original function, this function increases the running time monitoring function.
function query($Query_String, $halt_on_error = 1) {
$this->connect();
ss_timing_start();
$this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
ss_timing_stop();
ss_log(INFO, ss_timing_current(). Secs - .$Query_String);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if ($halt_on_error && !$this->Query_ID) {
$this->halt("Invalid SQL: ".$Query_String);
}
return $this->Query_ID;
}
2. Write beautiful code
1. Separate the background program from the front-end program
When writing PHP programs, some code is used to process some transactions, such as operating databases, performing mathematical operations, etc., while other codes are just displayed by the results of transaction processing, such as some PHP code that uses echo statements to display the results in HTML format on a web browser and those HTML code that directly embeds the PHP program. First of all, we should clearly distinguish these two codes, referring to the former as a background program and the latter as a front-end program.
Because PHP is an embedded programming language, that is, all PHP code can be embedded in HTML code, which brings many conveniences to the writing of programs. However, "everything will turn the opposite". If PHP code and HTML code are mixed in a long program, it will make the program messy and not conducive to the maintenance and reading of the program. Therefore, we need to port the PHP code mixed in HTML code in these programs as much as possible, encapsulate the code into functions in a special file, and then use the include statement in the HTML code to include these files, and call these functions in the appropriate location.
On the one hand, this approach makes both HTML code and PHP code simple and easy to read, and on the other hand, because HTML code needs to be updated continuously, this separation method can ensure that the background program will not be destroyed.
Unlike front-end programs, background programs pursue stability and structure more and rarely change, so they should be carefully designed and managed. In fact, it is worth investing a lot of time when designing a program. "Plant trees now and enjoy the cool future". In future design work, you will be able to easily use the background program written now.
2. Flexible use of included files
As mentioned earlier, the background program should be arranged in a series of included files. Include files can be loaded dynamically when needed through include statements, or pre-loaded in files by using the auto_prepend_file directive.
If the latter method is used, although the benefits of one and all are achieved, there are some disadvantages worth our attention. The following piece of code shows that it takes some time to analyze a huge inclusion file:
require();
ss_timing_start();
include();
ss_timing_stop();
echo
.ss_timing_current().
?>
In the above code, there is a 1000-line inclusion file. The result of the operation shows that it took 0.6 seconds to parse this inclusion file. For a large website, this speed is not negligible.
Another disadvantage of using included files is that if an error occurs in a statement in a file, the PHP program of the entire website will not run. So be careful when using it.
In fact, a little processing of the included file can make the included file parse only when needed. The following code makes the file parsed only when the program needs it:
if ( defined( __LIBA_INC) ) return;
define( __LIBA_INC, 1 );
/*
* Code...
*/
?>
3. Use object-oriented programming methods
PHP is also an object-oriented language. The object-oriented programming method is a software design method that excellent programmers highly recommend. In PHP programming, the advantages of object-oriented language can be fully utilized to encapsulate objects in programming. In the previous code, we used an object-oriented method. For example, when managing a database, we encapsulate the query() function into the database class, which greatly facilitates the management of the code and increases the readability of the program.
3. Pursuing program speed, not programming speed
In website construction, program running speed and web page download speed are important factors that affect success or failure. As a web programmer, you should pay more attention to the speed of your code running. The methods described below all increase the running speed of the code to varying degrees.
1. Use embedded HTML code instead of PHP's echo statements.
Because PHP is an embedded web programming language, HTML code and PHP code can be embedded with each other. However, many programmers are worried that using too much ""embedding PHP code in HTML will call the PHP interpreter multiple times, which reduces the running speed of PHP code, so they would rather use PHP echo statements to output HTML code than use HTML code directly. But the facts are exactly the opposite. Each PHP page only calls the PHP interpreter once to interpret all PHP code, so PHP code is only embedded when needed. Most of the time, using HTML code to input results directly will not only not reduce the running speed of the program, but also reduce the parsing of echo statements, which can often improve the running speed of the code.
Previous page123Next pageRead the full text