PHP has been quickly promoted with ease of use, but it is not easy to use it. In fact, many
Program It is easy for people to build Web application systems, but how many people have carefully considered their
Code Is it easy to maintain, robust, efficient, and secure? 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.Copy codeThe Code is as follows: <? PHP
/*
* The three faces of attributes
*/
Printf ("Pi is: %. 2fn <br> N", m_pi );
Printf ("Pi is also: %. 3fn <br> N", m_pi );
Printf ("Pi is also: %. 4fn <br> 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.
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:
Copy code The Code is as follows: 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:Copy codeThe Code is as follows: <? PHP
$ TMP = date ("f d, H: I a");/* ie January 3 */
Print $ TMP;
?>
It should be changed:Copy codeThe Code is as follows: <? PHP
Print date ("f d, H: I ");
?>
Another example:Copy codeThe Code is as follows: <? PHP
// String reverse_characters (string Str)
// Reverse all of the characters in a string.
Function reverse_characters ($ Str)
{
Return implode ("", array_reverse (preg_split ("//", $ Str )));
}
?>
It is not readable and can be changed:Copy codeThe Code is as follows: <? PHP
// 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 sideCopy codeThe Code is as follows: <? PHP shortde_once ("site. lib");?>
<HTML>
<Head>
<Title> <? PHP print_header ();?> </Title>
</Head>
<Body>
<H1> <? PHP print_header ();?> </H1>
<Table border = "0" cellpadding = "0" cellspacing = "0">
<Tr>
& Lt; TD width = "25%" & gt;
<? PHP print_links ();?>
</TD>
<TD>
<? PHP print_body ();?>
</TD>
</Tr>
</Table>
</Body>
</Html>
Site. lib the server side codeCopy codeThe Code is as follows: <? PHP
$ 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;
}
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 "ttt
<A href = "$ Links [$ I]"> $ Names [$ I] </a>
N <br> 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:Copy codeThe Code is as follows: <HTML>
<Head>
<Title> % page_title % </title>
</Head>
<Body % body_properties %>
<H1> % page_title %
<Table border = "0" cellpadding = "0" cellspacing = "0">
<Tr>
<TD width = "25%"> % page_links % </TD>
<TD> % page_content % </TD>
</Tr>
</Table>
</Body>
</Html>
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, or use the smarty template engine.
5. Do not use outdated Functions
As a free software, PHP has developed rapidly, and many of its functions are outdated. For example:Copy codeThe Code 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.