PHP is an efficient network programming language. it is a preferred language for Web programmers because of its flexible programming and fast running. A recent authoritative survey showed that 31.6% of websites now use PHP as the main server programming language. PHP is an efficient network programming language. it is a preferred language for Web programmers because of its flexible programming and fast running. A recent authoritative survey showed that 31.6% of websites now use PHP as the main server programming language.
However, it is not easy to become a PHP programming expert. As many people think, as long as they can quickly write a few simple codes to solve a complicated problem, they are PHP programmers, real PHP experts also need to consider more other issues. The following three principles should be followed by a mature PHP programmer in programming.
1. laziness is gold
2. write beautiful code
3. the pursuit of program speed rather than programming speed
1. laziness is gold
Be a lazy programmer? This is a strange idea! This is because the busiest person in the world may be a computer programmer. But it is because programmers are too busy that they should learn to be lazy during programming.
For a programmer, there are two lazy methods: first, boldly use the code of someone else's program to integrate the code into your own program or project. The second is to write some useful code to create a function library, which can be easily written in the future, saving a lot of repetitive work, so you can naturally be a little lazy.
Both of these methods are very suitable for PHP programmers.
First of all, PHP is a language born and grown in a free and open environment. There are thousands of programmers around the world who are constantly striving for PHP perfection and are willing to share their talents and code with others. Every day, you can find a lot of excellent program code from some PHP websites, email lists, and newsgroups. In this way, I do not encourage you to wait all day for others to write code for you, but you can "stand on the shoulders of great people" and fully carry forward "ism ", smart application of others' program code can save you a lot of time. Second, in PHP, you can easily create your own function library, which saves you a lot of trouble when writing programs later.
Below I will introduce several common functions, some of which come from some open source projects on the internet and some of which are selected from the mail list. If you can add them to your own function library, sooner or later you will find yourself benefiting.
1. common database processing functions
Compared with other CGI functions, one of the advantages of PHP is its powerful database processing capability. However, in PHP, some specific functions are used for different databases for special processing, and common database processing functions are missing. This greatly reduces the portability of program code, which also brings a lot of inconvenience to beginners.
On the Internet, many programmers solve this problem through encapsulation. They have compiled unified functions to deal with any popular databases-Mysql, which is popular in the Linux world, and SqlServer, which is widely used on Windows platforms. I personally like to use these functions, because some simple functions such as "query" and "next_record" can be used directly, you do not need to consider the complex things such as database connection and database handle, and do not need to consider what type of database is used.
If you need these functions, you can access the following URLs:
Https://www.php1.cn/
Https://www.php1.cn/
Https://www.php1.cn/
2. variable debugging functions
Debugging PHP programs has always been a headache. it does not have an integrated compilation and debugging environment like other advanced languages such as VB, and do not want Perl to run in a Linux or DOS environment. In fact, we can use echo statements flexibly to Debug PHP.
The following functions allow you to view the types and values of any variables in the program at any time.
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 (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 ;";
}
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 .")";
}
}
When necessary, simply add the following code to the program to view the types and values of the variables used in the program (including arrays and objects:
Echo ss_as_string ($ my_variable );
Using the following statement, we can directly view the values of all the variables in the program:
Echo ss_as_string ($ GLOBALS );
3. functions that control Log information
Another important method for debugging PHP programs is to view Log information. If you can easily control the Log information level and the Log information display content, it will bring more convenience to program debugging. The following functions can be easily implemented.
$ 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 );
}
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 Log level is lower than the preset level value. For example, add the following statement to the program:
Ss_log_set_level (INFO );
When running a PHP program, only LOG information at the ERROR and INFO level can be recorded and displayed, while DEBUG-level information is ignored. In addition, you can set the displayed information. The statement is 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 LOG information at any time:
Ss_log_reset ();
4. speed test functions
To optimize the code, we need a method that can test the code running time to select the best code. The following function can test the time required 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 we can easily check the execution time of any piece of code. we can even use multiple timers at the same time, you only need to set different parameters as the timer name when using the above functions.
5. debug and optimize database operations
For databases, the running speed is crucial. Although many books and articles teach you how to quickly run databases, all methods must be tested in practice. Next, we will combine the query () function in the PHPLib function library with the functions described above to compile a new query () function. compared with the original function, this function adds the runtime 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;
}