PHP problems in large-scale website development

Source: Internet
Author: User

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

Reference content is as follows:
<? Php
/*
* The three faces of π
*/

Printf ("pi is: %. 2f <br>", m_pi );
Printf ("pi is also: %. 3f <br>", m_pi );
Printf ("pi is also: %. 4f <br>", 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. String 6

   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:

Reference content is as follows:
<? Php
$ Tmp = date ("f d, h: I a");/* ie January 3 */
Print $ tmp;
?>

It should be changed:
 

Reference content is as follows:
<? Php
Print date ("f d, h: I ");
?>

Another example:

Reference content is 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 )));
}
?>

Can be changed:

Reference content is 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 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

Reference content is as follows:
<? Php shortde_once ("site. lib");?>
<Html>
<Head>
<Title> <? Php print_header ();?> </Title>
</Head>
<Body>
<H1> <? Php print_header ();?> </H1>
<Table border = "0" cellpadding = "0" cellspacing = "0">
<Tr>
& Lt; td width = "25%" & gt;
<? Php print_links ();?>
</Td>
<Td>
<? Php print_body ();?>
</Td>
</Tr>
</Table>
</Body>
</Html>

Site. lib? The server side code

Reference content is 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 ()));

$…… = @ 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; 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 );

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.