PHP programming development How to improve programming efficiency improve PHP programming technology, programming development programming Technology _php Tutorial

Source: Internet
Author: User
Tags echo command switch case

PHP programming development How to improve the efficiency of programming PHP programming technology, programming development programming technology


Using single quotes instead of double quotation marks to contain strings can be faster. Because PHP will search for variables in a string surrounded by double quotes, single quotes will not, note: only echo can do this, it is a string can be used as a parameter of the "function" (in the PHP manual that echo is a language structure, not a real function, so the function with double quotation marks).

1, if the method of the class can be defined as static, as far as possible to define as static, it will increase the speed of nearly 4 times times.

2. The speed of $row [' ID '] is 7 times times that of $row[id].

3. Echo is faster than print, and uses Echo's multiple parameters (referring to commas instead of periods) instead of string connections, such as Echo $str 1, $str 2.

4. To determine the maximum number of cycles before executing a for loop, do not calculate the maximum value once per cycle, preferably using foreach instead.

5. Unregister those unused variables, especially large arrays, in order to free up memory.

6, try to avoid the use of __get,__set,__autoload.

7, require_once () expensive.

8, include files as far as possible to use absolute path, because it avoids PHP to include_path to find the speed of the file, parsing the operating system path takes less time.

9. If you want to know when the script starts executing (that is, the server side receives the client request), use $_server[' request_time '] better than time ().

10, function instead of regular expression to complete the same function.

11, the Str_replace function is faster than the Preg_replace function, but the efficiency of the STRTR function is four times times the Str_replace function.

12, if a string substitution function, can accept an array or character as a parameter, and the parameter length is not too long, then you can consider an extra piece of replacement code, so that each pass argument is a character, rather than write a line of code to accept the array as the query and replace parameters.

13. Use the Select Branch statement (that is, switch case) better than using multiple if,else if statements.

14. Masking error messages with @ is extremely inefficient and extremely inefficient.

15, open the Apache mod_deflate module, can improve the browsing speed of the page.

16, the database connection should be turned off when used, do not use long connection.

17. Error messages are costly.

18, increment the local variable in the method, the speed is the quickest. is almost equivalent to the speed at which local variables are called in the function.

19, incrementing a global variable is twice times slower than incrementing a local variable.

20, incrementing an object property (such as: $this->prop++) is 3 times times slower than incrementing a local variable.

The following is to introduce the improvement of PHP programming technology.

I have decided to give you some caveats to improve your PHP code performance:

1. PHP tags

I know some people who write PHP code like to use the-<, but this is not a good habit, because the thumbnail tag on some servers can not be recognized correctly, and the use of standard PHP tags allows you to accurately compile your PHP code on any server. Maybe someday you'll need to install your code on a server that doesn't support thumbnail tags, so you'll have to spend one hours or more to sit down and upgrade your PHP code.

2, the debugging of PHP code

Sometimes we run into problems running PHP code and we don't know where the problem is. and PHP has a error_reporting () function that tells you every error in your code. If you want it to display all possible error messages for the page, you can put the following code in the second line of the file:

Php:

--------------------------------------------------------------------------------
Error_reporting (E_all);
--------------------------------------------------------------------------------

3. Debugging of PHP Code (supplemental)

If you complete a file with 1200 lines of PHP code and browse it in the browser, the error that displays your code appears on line No. 561 of the file. Now that you have an easy way to find the line, follow these steps:

--Create a new Notepad
--Copy your PHP code in.
--"edit", "Go to"
--Input "561", enter
--your mouse stays on line 561.
--See if there is an error near the line
--Fix the bug, re-upload the code into your space, and it will probably run normally. If there are any errors, repeat the above steps.
Wandering a niche add: now most use EditPlus and the like software, this method is outdated OH

4. Use annotations

If you have 1200 lines of PHP code, it is difficult to figure out what it is going to do, and the solution to this problem is to annotate your code.
PHP comments are different from HTML because it will not be output (because they are not even seen in "viewing source files")
There are three ways to add comments to PHP:

Php:

--------------------------------------------------------------------------------

<?php//Your comments//# Your comments/* Your comments */?>

--------------------------------------------------------------------------------

You can decorate them according to what you want, and you are the only one who uses them.

5, the PHP code indentation

I personally don't really like indenting PHP code, but it does make the code easy to read. When I have to indent, I use tab to do the following:

Php:

--------------------------------------------------------------------------------<?php//Settings//   $var 1 = "This",//Showing Variables//   if ($var 1 = = "This") {     echo "you said this";   } else{     echo "you said that";   }?>----------------------------------------------------------------------- --------

6, Fix your PHP file contains the way

I'm sure most of the people here will need to include one or two other files in a file. Ever wonder what to do if the file you need is not there? Does the person who browses your website feel that you are not professional?
In the PHP code I've written, I'll make sure it exists before I include another file, as in the following example:

Php:

7. Database Query

Sometimes your PHP code contains a connection to the database, you may encounter a little trouble, most people prone to database problems are in this form to write code:

Php:

。。 After running, he found that the data was not inserted into the database, and we can solve this problem:

Php:

8, abbreviations similar to if-then statements

If you receive data from a registration page and you want to make sure that all the information is filled in, you might use a statement that contains many if-then formats, like this:

Php:


Instead, you can actually merge the two lines by merging the If-then statement to make it only one line:

Php:


|| Have the same meaning as or,&& and and respectively

9. Use Echo or print?

Most people will say "Echo and print are the same", and I agree with this view. However, echo runs much faster than print and one letter less than print. The echo command appears later than print (I think), and obviously you know how to choose.

10. Enter a large section of HTML language

I believe a lot of people have a solution to this, but I still want to talk to you about some ways to solve this.
⑴, enter the end tag of the PHP, then you can enter the HTML code at will, then the PHP start tag (I do not like this, because it looks very unprofessional).
⑵, add backslashes in every HTML code (this is possible, but you always have to--every sentence).
⑶, using the Echo or print commands, that's all (recommended):

Php:

--------------------------------------------------------------------------------<?php//Showing a huge chunk of HTML at a time//echo<<
 
  

More HTML down here.

Centered text END;?>--------------------------------------------------------------------------------

I actually have a lot of other things to say about modifying PHP code, but that's it, I don't want to keep bothering you.
We hope to help you.

The above content is a small part of the PHP programming to introduce how to improve programming efficiency to improve the full description of PHP programming technology, I hope you like.

http://www.bkjia.com/PHPjc/1069351.html www.bkjia.com true http://www.bkjia.com/PHPjc/1069351.html techarticle PHP Programming Development How to improve the efficiency of programming PHP programming technology, programming and development programming technology with single quotes instead of double quotes to contain strings, this will be faster. Because PHP will be in double ...

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