PHP writing Large web site problem Set _php Tutorial

Source: Internet
Author: User
Tags php language value of pi smarty template
PHP with its ease of use is quickly promoted, but not easy to use it does not mean that it can be used, in fact, many programmers use it very easy to set up a Web application system, but how many people carefully consider their code, is easy to maintain, is strong enough, not efficient enough, is safe enough, This becomes a critical factor when PHP is used to build large web sites. Let's start with a slightly more subtle question until some fatal mistake. Altogether divided into three parts.
The first part, the lesser error

First, Printf (),
This function is primarily used to format the display of data. Use when you want to change the display format of a data.
For example, the value of Pi (3.1415926) is displayed with different precision.
Copy CodeThe code is as follows:
/*
* The three Faces ofπ
*/

printf ("Pi is:%.2FN
n ", M_PI);
printf ("Pi is also:%.3FN
n ", M_PI);
printf ("Pi is also:%.4FN
n ", M_PI);
?>

But many programmers use this function only to display some variable values and function return values. Because printf () formats the data at a slower speed before displaying it, the print and echo are applied only to show the data to increase speed.

Second, the semantic examination
PHP is a weakly typed language, meaning it is not defined before a variable is used, which makes programming a lot easier and more flexible, but you have to know exactly what kind of variable it should be, because the variable still actually corresponds to a certain type at run time (the various types can be freely converted to and from each other). A variable without a type does not exist. It is possible that PHP will not be able to check out your semantic errors, but because of variable type changes, some potential problems can occur. Another notable issue is the scope of the variable, which can also lead to some potential problems.
There are several basic variables in PHP:

Copy CodeThe code is as follows: Boolean, Resource, Integer, double, string, array and object

Third, the use of temporary variables
Misuse of temporary variables can result in a decrease in the efficiency of the program's operation. When to use temporary variables can be considered based on the following two points:
1, whether the variable is used at least two times.
2, whether the use of the variable will significantly improve the readability of the program.
If one is not satisfied, the use of the variable is omitted. For example:
Copy CodeThe code is as follows:
$tmp = Date ("F D, h:i a"); /* IE January 3, 5:00pm */
Print $tmp;
?>

It should be changed to:
Copy CodeThe code is as follows:
Print Date ("F D, h:i a");
?>

Another example:
Copy CodeThe code 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)));
}

?>

Its readability is not strong, can be changed to:
Copy CodeThe code 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-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 the program maintenance and redevelopment is very unfavorable, not suitable for large-scale site development. There are generally two ways to separate HTML from the PHP statement:
1, write a dedicated API, such as:

index.php the Client side
Copy CodeThe code is as follows:



<title><?php Print_header ();?></title>
















Site.lib the server side code
Copy CodeThe code 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 ()));

$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
$names [$i]
N
n ";
}
}
?>

This approach makes the program look more concise and executes faster.

2. How to use templates
This approach makes the program look more concise, as well as implementing the above functionality, using the following code:
Copy CodeThe code is as follows:


<title>%%page_title%%</title>


%%page_title%%







%%page_links%% %%page_content%%




Replace the content with the placeholder for the dynamically generated content, and then parse the template file with a parser, substituting the contents of the placeholder. Methods make it possible for a page maker to modify a template file even if it is not using PHP. The disadvantage of this approach is that the execution is inefficient because the template file is to be interpreted. At the same time, the implementation is more complex.

Note: The Fasttemplate class of www.thewebmasters.net can be conveniently implemented, or use the Smarty template engine

Five, do not use outdated functions
As a free software, PHP is developing quickly, and many of its functions are obsolete, such as:
Copy CodeThe code is as follows:
while (1):
print "5";
if ($idx + = = 5):
Break
endif
Endwhile;

Although it can still be used but the efficiency is not high, and may be disabled in later versions, causing the program to not run. Therefore, always check with the latest PHP manual that those functions are outdated and timely corrected.

http://www.bkjia.com/PHPjc/321838.html www.bkjia.com true http://www.bkjia.com/PHPjc/321838.html techarticle PHP for its ease of use to get a rapid promotion, but not easy to use it is not to say that it can be used, in fact, many programmers use it very easy to set up a Web application system, but how many people carefully test ...

  • 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.