Common PHP development habits

Source: Internet
Author: User
1. Use embedded HTML code instead of PHP echo statements. Because PHP is an embedded Web programming language, HTML code and PHP code can be embedded into each other. However, many programmers worry that the PHP interpreter will be called multiple times when embedded PHP code is used too much in HTML code, which reduces the running speed of PHP code. Therefore, they prefer to use PHP echo statements.

1. Use embedded HTML code instead of PHP echo statements. Because PHP is an embedded Web programming language, HTML code and PHP code can be embedded into each other. However, many programmers are worried that the PHP interpreter will be called multiple times when embedded PHP code is used too much in HTML code, thus reducing the running speed of PHP code. Therefore, they prefer to use the PHP echo statement.

1. Use embedded HTML code instead of PHP echo statements.

Because PHP is an embedded Web programming language, HTML code and PHP code can be embedded into each other. However, many programmers are worried that the PHP interpreter will be called multiple times when "" is used too much in HTML code to reduce the running speed of PHP code, therefore, we would rather use PHP echo statements to output HTML code rather than directly using HTML code. But the opposite is true. Every PHP page only calls the PHP interpreter once to explain all the PHP code. Therefore, the PHP code is embedded only when necessary, and most of the time the HTML code is used to input the result directly, this will not only reduce the program running speed, but also reduce the resolution of echo statements, which can often improve the code running speed.


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

Programmers who are familiar with Perl programming are more willing to use ereg_replace to complete string replacement, because the usage of ereg_replace in PHP is similar to that of pattern matching in Perl. However, the following code proves that replacing ereg_replace with str_replace can greatly improve the code running speed.



3. Pay attention to string references.

Like many other programming languages, PHP can use double quotation marks ("") to reference strings, or single quotation marks (). However, in PHP, if double quotation marks are used to reference a string, the PHP parser will first analyze whether there is any reference to the variable in the string. If there is a variable, the variable will be replaced. If it is a single quotation mark, it is not so complex-directly display all strings contained in single quotation marks. Obviously, using single quotes to reference string variables in PHP programming is faster than using double quotes.


4. determine the maximum number of cycles before executing the for loop. Do not calculate the maximum value for each loop.


5. Note the difference between include and require.

In PHP, The include () and require () functions are the same, but there are some differences in usage. include () is a conditional inclusion function, while require () it is an unconditional include function. For example, in the following example, if the variable $ somgthing is true, it will contain the file somefile
If ($ something ){
Include(inclusomefile.txt ");
}
However, no matter what value $ something takes, the following code will include the file somefile into the file:
If ($ something ){
Require(“somefile.txt ");
}


6. Avoid joint operations when performing database query operations

Compared with other Web programming languages, PHP databases are very powerful.
However, running a database in PHP is still a very time-consuming and laborious task. Therefore, as a Web programmer, we should minimize the number of database queries, at the same time, appropriate indexes should be created for the database.
Another thing worth noting is that when using PHP to operate a database, try not to use the union operation of multiple data tables. Even though the union operation can enhance the database query function, but it greatly increases the burden on the server.


7. if you want to know the TIME when the script starts to be executed (that is, when the SERVER receives a client REQUEST), use the $ _ SERVER ['request _ time'] function instead of the time () function.


8. Not necessarily object-oriented. Object-oriented is time-consuming. Some simple operations are still fast.


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


10. echo is faster than print and uses multiple echo parameters instead of periods to connect strings.

For example, echo $ str1 and $ str2.


11. When if... Else... Switch... should be selected if there are many nesting cases .... Case


12. Release unused mysql query results in time (mysql_free_result ())


13. Differences between isset () and empty ()

Both are used to test variables.
However, isset () is used to test whether a variable is assigned a value, while empty () is used to test whether a variable that has been assigned a value is null.
If a variable is not assigned a value, it is allowed to reference it in php, but there will be a notice prompt
If a variable is assigned a null value, $ foo = "" or $ foo = 0 or $ foo = false, empty ($ foo) returns true, isset ($ foo) returns the true value, that is, the null value will not cancel a variable.

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.