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 in PHP ereg_replace The usage 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
Like many other programming languages, PHP can use double quotes ("") to quote strings, or you can use single quotes. quotation marks(). 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 loop
5. Pay attention to include and require The difference
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 one. Contains functions. For example, in the following example, if the variable $somgthing is true, the file somefile will be included
if($something){
include("somefile.txt");
}
but no matter Whatever 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, you should try to avoid joint operations
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 echo's multiplexing Parameters (Translation: refers to using commas instead of periods) instead of string concatenation
such as echo $str1, $str2.
11. When there are many if...else... nestings, you should choose switch...case
12. Release the unused mysql query results in a timely manner ( mysql_free_result())
13. The difference between isset() and empty()
Both are used to test variables
But isset() tests whether the variable is Assignment, and empty() tests whether an assigned variable is empty
If a variable is not assigned a value, it is allowed to be referenced in PHP, but there will be a notice
If a variable is assigned an empty value Value, $foo="" or $foo=0 or $foo=false, then empty($foo) returns true, isset($foo) also returns true, which means assigning an empty value will not cancel a variable.
To unregister a variable, you can use unset($foo) or $foo=NULL