Php is quickly promoted with ease of use, but it is not easy to say that it can be used. In fact, many programmers use it to easily establish a web application system.
Php is quickly promoted with ease of use, but it is not easy to say that it can be used. In fact, many programmers use it to easily establish a web application system, but how many people have carefully considered their code, whether it is easy to maintain, whether it is robust enough, whether it is efficient enough, and whether it is safe enough, these are critical factors when php is used to build large websites. Next we will discuss from a minor issue to some fatal errors. It is divided into three parts.
Part 1 minor errors
I. printf (),
This function is mainly used to format and display data. It is used only when you want to change the display format of a data.
For example, the pi (3.1415926) value is displayed with different precision.
Reference content is as follows: /* * The three faces of π */
Printf ("pi is: %. 2f \ n \ N ", m_pi ); Printf ("pi is also: %. 3f \ n \ N ", m_pi ); Printf ("pi is also: %. 4f \ n \ N ", m_pi ); ?> |
However, many programmers only use this function to display some variable values and function return values. Because printf () needs to format the data at a low speed before displaying the data, it only applies print and echo to display the data to increase the speed. String 6
II. Semantic check
Php is a weak type language, that is, it does not need to be defined before using a variable, which brings great convenience and flexibility to programming, however, you must know the type of the variable, because the variable still corresponds to a certain type at runtime (various types can be freely converted to each other ), a variable of no type does not exist. It is possible that php does not check your semantic errors, but changes in the variable type may cause some potential problems. Another noteworthy problem is the scope of the variable, which may also lead to some potential problems.
Php has the following basic variables:
Boolean, resource, integer, double, string, array and object.
III. use of temporary variables
Misuse of temporary variables can reduce the program running efficiency. When to use temporary variables can be based on the following two considerations:
1. whether the variable is used at least twice.
2. Will the use of this variable significantly improve the readability of the program.
If either of them is true, the use of the variable is omitted. For example:
Reference content is as follows: $ Tmp = date ("f d, h: I a");/* ie January 3 */ Print $ tmp; ?> |
It should be changed:
Reference content is as follows: Print date ("f d, h: I "); ?> |
Another example:
Reference content is as follows: // String reverse_characters (string str) // Reverse all of the characters in a string. Function reverse_characters ($ str) { Return implode ("", array_reverse (preg_split ("//", $ str ))); } ?> |
Can be changed:
Reference content is as follows: // String reverse_characters (string str) // Reverse all of the characters in a string. Function reverse_characters ($ str) { $ Characters = preg_split ("//", $ str ); $ Characters = array_reverse ($ characters );
Return implode ("", $ characters ); } ?> |
IV. Separation of client and server code
The client and server code are actually html code and php language code in the php program. many people mix html and php statements in one file, making this file very large, this style is not suitable for the development of large websites because it is unfavorable for program maintenance and redevelopment. There are two methods to separate html and php statements:
1. write a dedicated api, for example:
Index. php? The client side
Reference content is as follows: <? Php print_header ();?>
|
Site. lib? The server side code
Reference content is as follows: $ Dbh = mysql_connect ("localhost", "sh", "pass ") Or die (sprintf ("cannot connect to mysql [% s]: % s ", Mysql_errno (), mysql_error ())); @ Mysql_select_db ("mainsite ") Or die (sprintf ("cannot select database [% s]: % s ", Mysql_errno (), mysql_error ()));
$…… = @ Mysql_query ("select * from site", $ dbh) Or die (sprintf ("cannot execute query [% s]: % s ", Mysql_errno (), mysql_error ()));
$ Site_info = mysql_fetch_object ($ something );
Function print_header () { Global $ site_info; Print $ site_info-> header; string 4 }
Function print_body () { Global $ site_info; Print nl2br ($ site_info-> body ); }
Function print_links () { Global $ site_info;
$ Links = explode ("\ n", $ site_info-> links ); $ Names = explode ("\ n", $ site_info-> link_names );
For ($ I = 0; $ I <count ($ links); $ I ++) { Print "\ t $ Names [$ I] \ N \ N "; } } ?> |
This method makes the program look simple and fast.
2. use the template
This method makes the program look more concise and implements the above functions. the following code is available:
Reference content is as follows: String 8 % Page_title % % Page_title %
% Page_links % |
% Page_content % |
|
Use a placeholder to replace the content to be dynamically generated. then use a parsing program to analyze the template file and replace the placeholder with the actual content. You can modify the template file even if you do not use php. The disadvantage of this method is that the execution efficiency is not high because the template file needs to be interpreted. It is also complicated to implement.
Note: The fasttemplate class of www.thewebmasters.net can conveniently implement the above functions.
5. do not use outdated functions
As a free software, php has developed rapidly, and many of its functions are outdated. for example:
Reference content is as follows: While (1 ): Print "5 "; If ($ idx ++ = 5 ): Break; Endif; Endwhile; |
Although it is still usable, the efficiency is certainly not high, and may be disabled in later versions, resulting in program failure. Therefore, check the latest php Manual for timely correction of functions that are out of date.