Collection of php compilation of large-scale website problem set _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Collect php to compile a large website issue set. 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 tested PHP for its ease of use and rapid promotion, but it does not mean that it can be used well. In fact, many programmers can easily establish a WEB application system with it, 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.
/*
* The three faces of attributes
*/

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.

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:
$ Tmp = date ("F d, h: I a");/* ie January 3 */
Print $ tmp;
?>
It should be changed:
Print date ("F d, h: I ");
?>

Another example:

// 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:

// 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



<? Php print_header ();?>

















Site. lib? The server side code



$ 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 "\ 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:


% 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:

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.

...

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.