Some problems in the development of large Web sites by PHP _php tutorial

Source: Internet
Author: User
Tags php language value of pi

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.

The following is the referenced content:
/*
* 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 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. String 6

   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:

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:

The following is the referenced content:
$tmp = Date ("F D, h:i a"); /* IE January 3, 5:00pm */
Print $tmp;
?>

It should be changed to:

The following is the referenced content:
Print Date ("F D, h:i a");
?>

Another example:

The following is the referenced content:
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 change to:

The following is the referenced content:
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

The following is the referenced content:



<?php Print_header ();?>














Site.lib? The server side code

The following is the referenced content:
$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; String 4
}

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);

http://www.bkjia.com/PHPjc/531681.html www.bkjia.com true http://www.bkjia.com/PHPjc/531681.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.