PHP Development Considerations Summary _php Tips

Source: Internet
Author: User
Tags mysql query php programming

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

Copy Code code as follows:

<!--P
Don't do this.
for ($i =0; $i <=count ($array); $i + +) {
...
}
It should be done.
$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

Copy Code code as follows:

if ($something) {
Include ("Somefile.txt");
}
But no matter what value the $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 Switch....case

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.

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.