PHP Development Considerations Summary

Source: Internet
Author: User
Tags mysql query php code php programming require

1. Use embedded HTML code, not PHP's echo statement.

Because PHP is an embedded web programming language, you can embed HTML code and PHP code into each other. But many programmers are concerned about the excessive use of "" embedded PHP code in the HTML code will invoke the PHP interpreter multiple times, thus reducing the speed of PHP code, so prefer to use the PHP echo statement to output HTML code, rather than directly using HTML code. But the exact opposite is true. Each PHP page only calls once PHP interpreter to explain all the PHP code, so, only when needed to embed PHP code, and most of the time directly using the HTML code input results, not only will not reduce the speed of the program, but also because of the reduction of the Echo statement parsing, You can often improve the speed of your code.

2. Try to use Str-replace instead of Ereg-replace

Programmers who are accustomed to programming with Perl are more willing to use ereg_replace to complete string substitution, because ereg_replace usage in PHP is similar to that of pattern matching in Perl. However, the following code demonstrates that using Str_replace instead of ereg_replace can greatly improve the speed of your code.

3. Note the reference to the string

PHP, like many other programming languages, can use double quotes ("") to refer to strings, or to use single quotes (). But in PHP, if you use double quotes to refer to a string, the PHP parser will first parse the string for a reference to a variable, and then replace the variable with a variable. If it's a single quote, it's not so complicated--directly displays all the strings that are enclosed in single quotes. Obviously, in PHP programming, using single quotes to refer to string variables is quicker than using double quotes.

4. Determine the maximum number of loops before executing the For loop, and do not calculate the maximum value per loop at once

The code is as follows:


/Don't do this

for ($i =0; $i <=count ($array); $i + +) {



//should do so

$array _length = count ($array);

for ($i =0; $i <= $array _length; $i + +) {




5. Note the difference between include and require

In PHP, include () and require () have the same functionality, but there are some differences in usage, include () is a conditional inclusion function, and require () is an unconditional inclusion function. For example, in the following example, if the variable $somgthing is true, the file will be included Somefile

The code is as follows:

if ($something) {

include ("Somefile.txt");


but no matter what value $something takes, the following code will include the file Somefile in the file:

if ($something) {

require ("Somefile.txt");


6. When doing database query operation, should avoid joint operation as far as possible

PHP's database functions are powerful compared to other Web programming languages.
But in PHP, the database operation is still a very time-consuming and laborious thing, so as a web programmer, to minimize the database query operations, but also to establish an appropriate index for the database.
Another noteworthy thing is that when using PHP to manipulate a database, it is possible to not use a joint operation with multiple tables, although joint operations can enhance the query function of the database, but greatly increase the burden on the server.

7. If you want to know when the script starts to execute (that is, the server receives the client request), it's better to use the time () function with $_server[' request_time '.

8. Object-oriented is not necessarily required, and object-oriented is more time-consuming. Some simple operation or process is fast.

9. The speed of the $row [' ID '] is 7 times times $row[id]

10.echo is faster than print and uses Echo's multiple arguments to replace string concatenation with a comma instead of a period

Like Echo $str 1, $str 2.

11. When If...else ... Nested more, you should choose

12. Will not use the MySQL query result to be released in Time (Mysql_free_result ())

The difference between 13.isset () and Empty ()

Both are used for test variables.
But Isset () is the test whether the variable is assigned, and empty () is to test whether a variable that has already been assigned is empty
If a variable is not assigned to the reference in PHP is allowed, but there will be notice hint
If a variable is null, $foo = "" or $foo=0 or $foo =false, then empty ($foo) returns True, Isset ($foo) returns True, which means that the null value does not unregister a variable.

To unregister a variable, you can use Unset ($foo) or $foo=null

The above is the entire content of this article, I hope you can enjoy.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.