PHP Write a large web site problem set (from Yesky)

Source: Internet
Author: User
Tags date explode functions php language variables sprintf variable value of pi
Problem PHP Write large web site problem set


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.
<?php
/*
* The three Faces ofπ
*/

printf ("Pi is:%.2f


", M_PI);
printf ("Pi is also:%.3f


", M_PI);
printf ("Pi is also:%.4f


", 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:
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:
<?php
$tmp = Date ("F D, h:i a"); /* IE January 3, 2:30 pm * *
Print $tmp;
?>
It should be changed to:
<?php
Print Date ("F D, h:i a");
?>

Another example:
<?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));
}

?>
The readability is not strong, can be changed to:
<?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
<?php include_once ("Site.lib");?>
<title> <?php Print_header ();?> </title>
<body>
<table border= "0" cellpadding= "0" cellspacing= "0" >
<tr>
&LT;TD width= "25%" >
<?php print_links ();?>
</td>
<td>
<?php print_body ();?>
</td>
</tr>
</table>
</body>


Site.lib? The server side code


<?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 ("
", $site _info->links);
$names = Explode ("
", $site _info->link_names);

for ($i = 0; $i < count ($links); $i + +)
{
Print "
<a href= "$links [$i]" > $names [$i]</a>
 


";
}
}
?>

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:
<title>%%PAGE_TITLE%%</title>
<body%%body_properties%%>
<table border= "0" cellpadding= "0" cellspacing= "0" >
<tr>
&LT;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 realize the above functions.

V. Do not use outdated functions
As a free software, PHP is developing quickly, and many of these functions are obsolete, such as:

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.