How to improve programming efficiency and improve PHP programming technology in PHP programming development

Source: Internet
Author: User
This article mainly introduces how PHP programming development can improve programming efficiency (remember these 20 points). For more information, see

This article mainly introduces how PHP programming development can improve programming efficiency (remember these 20 points). For more information, see

Use single quotes instead of double quotes to include strings, which is faster. Because PHP will search for variables in strings enclosed by double quotes, but not in single quotes. Note: Only echo can do this, it is a "function" that can treat multiple strings as parameters. echo is a language structure, not a real function, therefore, the function is added with double quotation marks ).

1. If the class method can be defined as static, it should be defined as static as much as possible, and its speed will be increased by nearly four times.

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

3. echo is faster than print, and multiple echo parameters are used to replace string connections, such as echo $ str1, $ str2.

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

5. Cancel unnecessary variables, especially large arrays, to release the memory.

6. Avoid using _ get ,__ set ,__ autoload whenever possible.

7. require_once () is expensive.

8. Try to use the absolute path when you include the file, because it avoids PHP's speed of searching for the file in include_path, and it takes less time to parse the operating system path.

9. If you want to know the TIME when the script starts to be executed (I .e. when the SERVER receives a client REQUEST), use $ _ SERVER ['request _ time'] instead of time ().

10. functions use the same functions instead of regular expressions.

11. The str_replace function is faster than the preg_replace function, but the strtr function is four times more efficient than the str_replace function.

12. If a string replacement function can take an array or character as a parameter and the parameter length is not too long, you can consider writing an additional replacement code so that each parameter passing is a character, instead of writing only one line of code to accept arrays as query and replacement parameters.

13. Using the select branch statement is better than using multiple if and else if statements.

14. Blocking error messages with @ is very inefficient and extremely inefficient.

15. Open the mod_deflate module of apache to speed up web page browsing.

16. When the database connection is used up, it should be switched off. Do not use persistent connections.

17. The error message is expensive.

18. Increase the local variable in the method at the fastest speed. It is almost the same as calling a local variable in a function.

19. Increasing a global variable is twice slower than increasing a local variable.

20. Incrementing an object property (for example, $ this-> prop ++) is three times slower than incrementing a local variable.

The following describes how to improve PHP programming technology.

I decided to tell you some notes that can improve the PHP code performance:

1. PHP labels

I know some people prefer to use scaling labels when writing PHP code-<? ?>, But this is not a good habit, because scaling labels cannot be correctly identified on some servers, and the use of standard PHP labels allows you to accurately compile your PHP code on any server. One day, you may need to install your code on servers that do not support scaling labels, so you will have to spend an hour or more time sitting down and upgrading your PHP code.

2. PHP code debugging

Sometimes we encounter problems when running PHP code, and we don't know where the problem is. In PHP, there is an error_reporting () function, which tells you every error in your code. If you want it to display all possible error messages on the page, you can put the following code in the second line of the file:


Error_reporting (E_ALL );

3. PHP code debugging (Supplement)

If you have completed a file with 1200 Lines of PHP code and browsed it in a browser, the error of your code appears in the 561st line of the file. In this case, you have a simple method to find the row, follow these steps:

-- Create a notebook
-- Copy your PHP code
-- "Edit"-> "go"
-- Enter "561" and press Enter.
-- Your mouse is stuck at 561 rows.
-- Check for errors near this row
-- Fixed the error and re-uploaded the code to your space, which is likely to run normally. If there are any errors, repeat the preceding steps.
Xiaoyi Xiaosheng added: currently, most of the software such as editplus is used. This method is outdated.

4. Use annotations

If your PHP Code contains 1200 lines, it is very difficult to understand what it is to do. The solution to this problem is to add comments to your code.
PHP annotations are different from those in HTML Because it will not be output (thought they will not even be seen when "viewing source files)
There are three methods to add comments in PHP:



<? Php // your comments // # Your comments/* your comments */?>


You can decorate them as you wish. You are the only one who uses them.

5. PHP code indentation

I personally do not like to indent PHP code, but it does make the code easy to read. When I have to indent, I use a tab, as shown below:


-------------------------------------------------------------------------------- <? Php // Settings // $ var1 = "This"; // Showing Variables // if ($ var1 = "This") {echo "You said This ";} else {echo "You said That" ;}?> -------------------------------------------------------------------------------

6. Modify the PHP File Inclusion Method.

I'm sure most people here need to include one or two other files in one file. Have you ever wondered what to do if the file you need does not exist? Will the person who browsed your webpage feel that you are not professional enough?
In my PHP code, I will determine whether another file exists before it is included, as shown in the following example:


-------------------------------------------------------------------------------- <? Php if (! File_exists ("layout. inc. php") {exit ("Error: LayOut File Missing");} else {include_once ("layout. inc. php");}?> --------------------------------------------------------------------------------

7. Database Query

Sometimes your PHP Code contains database connections, and you may encounter some minor troubles. Most people who are prone to database problems write code in this form:


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: 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.