Home > Backend Development > PHP Tutorial > Summary of PHP development precautions, PHP development precautions_PHP tutorial

Summary of PHP development precautions, PHP development precautions_PHP tutorial

WBOY
Release: 2016-07-13 10:08:03
Original
814 people have browsed it

Summary of PHP development precautions, PHP development precautions

1. Use embedded HTML code instead of PHP’s echo statement.

Because PHP is an embedded web programming language, HTML code and PHP code can be embedded in each other. However, many programmers are worried that excessive use of "" to embed PHP code in HTML code will call the PHP interpreter multiple times, thereby reducing the running speed of PHP code, so they would rather use PHP's echo statement to output HTML code instead of directly Use HTML code. But the truth is exactly the opposite. Each PHP page only calls the PHP interpreter once to interpret all PHP codes. Therefore, the PHP code is only embedded when needed, and most of the time, the HTML code is used directly to input the results. Not only will it not reduce the running speed of the program, but it will also Because the parsing of echo statements is reduced, the running speed of the code can often be improved.

2. Try to use str-replace instead of ereg-replace

Programmers who are used to programming in Perl are more willing to use ereg_replace to complete string replacement work, because the usage of ereg_replace in PHP is similar to the usage of pattern matching in Perl. However, the code below proves that using str_replace instead of ereg_replace will greatly improve the speed of the code.

3. Pay attention to string references

PHP, like many other programming languages, can use double quotes ("") to quote strings, or single quotes (). But in PHP, if you use double quotes to quote a string, the PHP parser will first analyze whether there is a reference to a variable in the string. If there is a variable, it will replace the variable. If it is single quotes, it is not so complicated - all strings enclosed in single quotes are directly displayed. Obviously, in PHP programming, it is faster to use single quotes to quote string variables than double quotes.

4. Determine the maximum number of loops before executing the for loop, do not calculate the maximum value every time it loops

Copy code The code is as follows:


5. Pay attention to the difference between include and require

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

will be included

Copy code The code is as follows:

if($something){
Include(“somefile.txt”);
}
But no matter what the value of $something is, the following code will include the file somefile into the file:
if($something){
​ require(“somefile.txt”);
}

6. When doing database query operations, joint operations should be avoided as much as possible

Compared with other web programming languages, PHP's database function is very powerful.
However, running the database in PHP is still a very time-consuming and labor-intensive matter. Therefore, as a Web programmer, you must minimize database query operations and establish appropriate indexes for the database.
Another thing worth noting is that when using PHP to operate a database, try not to use joint operations of multiple data tables. Although joint operations can enhance the query function of the database, it greatly increases the burden on the server.

7. If you want to know the time when the script starts executing (annotation: that is, the server receives the client request), it is better to use $_SERVER['REQUEST_TIME'] instead of using the time() function.

8. It does not have to be object-oriented, object-oriented is more time-consuming. Some simple operations are still a quick process.

9.$row['id'] is 7 times faster than $row[id]

10.echo is faster than print, and uses multiple parameters of echo (Translation: refers to using commas instead of periods) instead of string concatenation

For example echo $str1,$str2.

11. When there are many nestings of if…else…, you should choose switch….case

12. Release unused mysql query results in a timely manner (mysql_free_result())

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

Both are used for testing variables
But isset() tests whether a variable has been assigned a value, and empty() tests whether a variable that has been assigned a value is empty
If a variable is referenced in PHP without being assigned a value, it is allowed, but there will be a notice
If a variable is assigned a null value, $foo="" or $foo=0 or $foo=false, then empty($foo) returns true and isset($foo) also returns true, which means assigning a null value will not log out. 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 all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/953144.htmlTechArticleSummary of PHP development considerations, PHP development considerations 1. Use embedded HTML code instead of PHP’s echo statement. Because PHP is an embedded Web programming language, you can combine HTML code with PH...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template