Teach you how to become a PHP expert
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;&nbsp;&nbsp;&nbsp;" ; } $str .= $var . ==> ; $str .= ss_as_string( $val , $column +1). " n" ; } for ( $i = 0; $i < $column ; $i ++){ $str .= "&nbsp;&nbsp;&nbsp;&nbsp;" ; } return $str .); } function ss_object_as_string (& $object , $column = 0) { if ( empty empty ( $object ->classname)) { return "$object" ; } else { $str = $object ->classname. "( n" ; while (list(, $var ) = each( $object ->persistent_slots)) { for ( $i = 0; $i < $column ; $i ++){ $str .= "&nbsp;&nbsp;&nbsp;&nbsp;" ; } global $ $var ; $str .= $var . ==> ; $str .= ss_as_string($ $var , column+1). " n" ; } for ( $i = 0; $i < $column ; $i ++){ $str .= "&nbsp;&nbsp;&nbsp;&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 . ")" ; } }
$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 ]) { //Do not display log information 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 ); }
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 ; }
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; } 1. Separate the background program from the front-end program
When writing PHP programs, some codes are used to deal with some transactions, such as operating databases and performing mathematical operations, while others are only used to display the results of transaction processing, such as some PHP codes that use echo statements to display the results in HTML format on the Web browser, and those HTML codes that are directly embedded in PHP programs. First of all, we should clearly distinguish these two kinds of codes, and call the former background program and the latter front-end program.
Because PHP is an embedded programming language, that is to say, all PHP code can be embedded into HTML code, which brings many conveniences for programming. However, "things turn against each other when they reach the extreme". If PHP code and HTML code are mixed in a long program, it will make the program disorderly and not conducive to program maintenance and reading.
So we need to transplant the PHP code mixed with HTML code from these programs as much as possible, encapsulate these codes into functions in special files, and then use the include statement to include these files in HTML code, and call these functions at appropriate locations.
On the one hand, this method makes HTML code and PHP code easy to read, and on the other hand, because HTML code needs to be updated constantly, this separation method can ensure that the background program will not be damaged. Different from the front-end program, the back-end program is more stable and structured, with few changes, so it should be carefully designed and managed. In fact, it is worthwhile to invest a lot of time in designing the platform program. "Plant trees now and enjoy the cool in the future" will make it easy to use the background program written now in future design work.
2. Flexible use of included files
As mentioned earlier, the daemon should be arranged in a series of include files. The include file can be loaded dynamically when needed through the include statement, or automatically loaded in advance in the php.ini file by using the auto_prepend_file command. If the latter method is used, although the benefits are obtained once and for all, there are also some disadvantages worth our attention. The following code shows that parsing a large include file takes a certain amount of time:
require(timing.inc);
ss_timing_start();
include(test.inc);
ss_timing_stop();
echo
.ss_timing_current().
;
In the above code, test.inc is a 1000 line include file. The running results show that it took 0.6 seconds to parse the include file. For a large website, this speed is not negligible. Another disadvantage of using include files is that if an error occurs in a statement in a file, the PHP program of the entire website will not run. So it is very careful to use it. In fact, by slightly processing the include file, the include file can be parsed only when needed. The following code makes the abc.inc file be 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 highly praised by excellent programmers. In PHP programming, the advantages of object-oriented language can be fully used to encapsulate the objects in programming. In the previous code, we used the object-oriented method. For example, when managing the database, we encapsulated the query() function into the database class, which greatly facilitated the code management and increased the readability of the program. Pursue program speed, not programming speed
In website construction, both the running speed of the program and the download speed of the webpage are important factors for success or failure. As a Web programmer, you should pay more attention to the running speed of the code. The following methods have improved the running speed of the code to varying degrees.
1. Use embedded HTML code instead of PHP echo statements.
Because PHP is an embedded Web programming language, you can embed HTML code and PHP code into each other. However, many programmers worry that too much use of "" embedded PHP code in HTML code will call the PHP interpreter many times, which will reduce the running speed of PHP code. Therefore, they prefer to use PHP echo statements to output HTML code instead of directly using HTML code.
But the opposite is true. Each PHP page only calls the PHP interpreter once to interpret all PHP code. Therefore, PHP code is embedded only when needed. Most of the time, using HTML code directly to input results will not reduce the running speed of the program, and because the parsing of echo statements is reduced, the running speed of the code can often be improved. The following code proves our conclusion. In this code, we use the time test function described earlier.
2. Use str replace instead of ereg replace
Programmers accustomed to programming in Perl are more willing to use ereg_replace to complete string replacement, because the usage of ereg_replace in PHP is similar to that of pattern matching in Perl. However, the following code proves that using str_replace instead of ereg_replace can greatly improve the running speed of the code. Test the running speed of str_replace and ereg_replace:
//This code tests the running speed of str_replace emphasis;?>
for ($i=0; $i<1000; $i++) {
str_replace(i>, b>, $string).
;
}
//This code tests the running speed of ereg_replace
for ($i=0; $i<1000; $i++) {
ereg_replace(<([/]*)i>, <\1b>, $string).
;
}
3. Note the reference of string
PHP, like many other programming languages, can use double quotation marks ("") to reference strings, or single quotation marks (). However, in PHP, if double quotation marks are used to reference strings, the PHP parser will first analyze whether there are references to variables in the strings. If there are variables, the variables will be replaced. If it is a single quotation mark, it is not so complicated - all strings contained in the single quotation mark are displayed directly. Obviously, in PHP programming, using single quotation marks to reference string variables is faster than using double quotation marks.
4. Avoid using union operations in the database
Compared with other Web programming languages, PHP's database function is very powerful. However, the database operation in PHP is still a time-consuming and laborious thing. Therefore, as a Web programmer, you should try to reduce the query operation of the database, and at the same time, you should establish an appropriate index for the database.
Another thing worth noting is that when using PHP to operate the database, try not to use the joint operation of multiple data tables. Although the joint operation can enhance the query function of the database, it greatly increases the burden of the server. To illustrate this problem, we can take a look at the following simple example.
We created two data tables foo and big_foo in the database. In the data table foo, there is only one field, which contains all natural numbers from 1-1000. The data table big_foo also has only one field, but contains all natural numbers from 1-1000000. Therefore, in terms of size, big_foo is equal to that foo and itself are jointly operated.
$db->query("select * from foo");
0.032273 secs
$db->next_record();
0.00048999999999999 secs
$db->query("insert into foo values (NULL)");
0.019506 secs
$db->query("select * from foo as a, foo as b");
17.280596 secs
$db->query("select * from foo as a, foo as b where a.id > b.id");
14.645251 secs
$db->query("select * from foo as a, foo as b where a.id = b.id");
0.041269 secs
$db->query("select * from big_foo");
25.393672 secs
From the above operation results, we can find that the speed of combining two data tables with 1000 records is not much faster than that of a single large data table with 1000000 records.
5. Note the difference between include and require
In PHP, include() has the same function as require(), but there are some differences in usage. include() is a conditional function, while require() is an unconditional function. For example, in the following example, if the variable $somgthing is true, the file somefile will be included:
if($something){
include("somefile");
}
Regardless of the value of $something, the following code will include the file somefile into the file:
if($something){
require("somefile");
}
The following interesting example fully illustrates the difference between the two functions.
$i = 1;
while ($i < 3) {
require("somefile.$i");
$i++;
}
In this code, the program will include the same file every time it loops. Obviously, this is not the original intention of the programmer. From the code, we can see that this code hopes to include different files in each cycle. To complete this function, you must turn to the function include();
$i = 1;
while ($i < 3) {
include("somefile.$i");
$i++;
}
6. Note the difference between echo and print
The functions of echo and print in PHP are basically the same, but there are slight differences between them. In PHP code, print can be used as a common function. For example, after executing the following code, the value of the variable $res will be 1.
$ret = print "Hello World";
This means that print can be used in some complex expressions, but echo cannot. Similarly, the echo statement runs slightly faster than the print statement in code, because the echo statement does not require any value to be returned.