Home > Article > Backend Development > PHP development experience summary
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 using HTML codes directly 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 quoting
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 loop
5. Pay attention to the difference between include and require
In PHP programming, include() and require() have the same functions, but there are some differences in usage. include() is conditional Contains functions, while require() unconditionally includes 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 what value $something takes, the following The 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 In terms of language, 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 doesn’t 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 (Annotation: refers to using commas instead of periods) instead of string concatenation
For example, echo $str1,$str2.
11. When if...else... are nested a lot, 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 to test variables
But isset() tests whether a variable has been assigned a value, while empty() tests whether a variable that has been assigned a value is empty
If a variable is not assigned a value, it is referenced in PHP. Allowed, but there will be a notice
If a variable is assigned an empty value, $foo="" or $foo=0 or $foo=false, then empty($foo) returns true and isset($foo) also returns true , which means that assigning a null value will not unregister a variable.
To unregister a variable, you can use unset($foo) or $foo=NULL
The above has introduced a summary of PHP development experience, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.