Home > Backend Development > PHP Tutorial > Summary of PHP development considerations_PHP tutorial

Summary of PHP development considerations_PHP tutorial

WBOY
Release: 2016-07-13 10:08:05
Original
1407 people have browsed it

Summary of PHP development considerations

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, embedding PHP codes only when needed, and most of the time directly using HTML codes to input results, will not only not reduce the running speed of the program, but 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 following code 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

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

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: the server receives the client request), it is better to use $_SERVER[‘REQUEST_TIME’] than 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 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/952854.htmlTechArticleSummary of PHP development considerations 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. But...
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