PHP is quickly promoted by its ease of use, but ease of use is not to say that it can be used, in fact many programmers use it easy to set up a Web application system, but how many people carefully consider their code, whether easy to maintain, strong enough, not efficient enough, safe enough, These are key factors when PHP is used to build large web sites. Let's start with some minor questions until some fatal mistakes. is divided into three parts.
The first part, the minor mistake
One, Printf (),
This function is primarily used to format display data. Use when you want to change the display format of a particular data.
For example, the value of Pi (3.1415926) is displayed with different precision.
Copy Code code as follows:
<?php
/*
* The three Faces ofπ
*/
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);
?>
But many programmers use this function only to show some variable values and function return values. Because printf () formats the data to be slow before displaying the data, you can increase the speed by applying print and echo only to display the data.
Second, the semantic examination
PHP is a weak type language, that means that you don't have to define a variable before you use it, which makes it a lot easier and more flexible, but you have to know exactly what type of variable it should be, because it still actually corresponds to a certain type at run time (the various types are free to convert to each other), There is no type of variable that does not exist. It is possible that PHP does not check out your semantic errors, but because of variable type changes, it can lead to some potential problems. Another noteworthy problem is the scope of the variable, which may also lead to some potential problems.
There are several basic variables in PHP:
Copy Code code as follows:
Boolean, Resource, Integer, double, string, array and object
Third, the use of temporary variables
The misuse of temporary variables can result in lower efficiency of program operation. When to use temporary variables is based on the following two points to consider:
1, whether the variable is used at least two times.
2, the use of this variable will significantly improve the readability of the program.
If one is not satisfied, the use of the variable is omitted. For example:
Copy Code code as follows:
<?php
$tmp = Date ("F D, h:i a"); /* IE January 3, 2:30 pm * *
Print $tmp;
?>
It should be changed to:
Copy Code code as follows:
<?php
Print Date ("F D, h:i a");
?>
Another example:
Copy Code code 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));
}
?>
Its readability is not strong, can be changed to:
Copy Code code 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-side code
Client and server-side code in the PHP program is actually HTML code and PHP language code, many people put HTML and PHP statements mixed in a file, making this file is very large, this style of program maintenance and redevelopment is unfavorable, not suitable for large-scale site development. There are generally two ways to separate HTML and PHP statements:
1, write a special API, such as:
index.php the Client side
Copy Code code as follows:
<?php include_once ("Site.lib");?>
<title> <?php Print_header ();?> </title>
<body>
<table border= "0" cellpadding= "0" cellspacing= "0" >
<tr>
<TD width= "25%" >
<?php print_links ();?>
</td>
<td>
<?php print_body ();?>
</td>
</tr>
</table>
</body>
Site.lib the server side code
Copy Code code 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 ());
$sth = @mysql_query ("SELECT * from Site", $DBH)
Or Die (sprintf ("Cannot execute query [%s]:%s",
Mysql_errno (), mysql_error ());
$site _info = mysql_fetch_object ($sth);
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 approach makes the program look simpler and executes faster.
2, using the template method
This method makes the program look more concise and also implements the above functionality, using the following code:
Copy Code code as follows:
<title>%%PAGE_TITLE%%</title>
<body%%body_properties%%>
<table border= "0" cellpadding= "0" cellspacing= "0" >
<tr>
<TD width= "25%" >%%PAGE_LINKS%%</td>
<td>%%PAGE_CONTENT%%</td>
</tr>
</table>
</body>
Use placeholders instead of dynamically generated content, then parse the template file with an analytic program to replace the placeholder with the content. Way to make template files even if you don't use PHP as a page maker. The disadvantage of this approach is that it is inefficient to perform because the template file is interpreted. It is also more complex to implement.
Note: The Fasttemplate class of www.thewebmasters.net can easily implement the above functions, or use the Smarty template engine
V. Do not use outdated functions
As a free software, PHP is developing quickly, and many of these functions are obsolete, such as:
Copy Code code as follows:
while (1):
print "5";
if ($idx + + = 5):
Break
endif
Endwhile;
Although it is still available, it is certainly not efficient and may be disabled in later versions, causing the program to run. So it's always a good time to check those functions against the latest PHP manual.