PHP code performance optimization tips

Source: Internet
Author: User
Although PHP is powerful, it does not have any disadvantages. Like other languages, it also needs to constantly update and modify the compiled code in actual application writing until it is satisfied. Next we will share with you some tips on optimizing PHP code performance. PHP code performance optimization 1. Do not copy variables at will. Sometimes, to make PHP code more

Although PHP is powerful, it does not have any disadvantages. Like other languages, it also needs to constantly update and modify the compiled code in actual application writing until it is satisfied. Next we will share with you some tips on optimizing PHP code performance. PHP code performance optimization 1. Do not copy variables at will. Sometimes, to make PHP code more

Although PHP is powerful, it does not have any disadvantages. Like other languages, it also needs to constantly update and modify the compiled code in actual application writing until it is satisfied. Next we will share with you some tips on optimizing PHP code performance.

PHP code performance optimization 1. Do not copy variables at will

Sometimes, to make PHP code more clean, some new PHP users (including me) will copy the predefined variables to a variable with a shorter name, in fact, the result is a doubling of the memory consumption, which only slows down the program. In the following example, if a user maliciously inserts KB of text into the text input box, 1 MB of memory will be consumed!

  1. BAD:
  2. $ Description=$ _ POST ['description'];
  3. Echo$ Description;
  4. GOOD:
  5. Echo$ _ POST ['description'];

Reference

PHP code performance optimization 2. Use single quotes for strings

The PHP engine allows single quotes and double quotes to encapsulate string variables, but there is a big difference! The double quotation mark string is used to tell the PHP engine to read the string content, find the variable, and change it to The value corresponding to the variable. Generally, strings do not have variables, so using double quotation marks will lead to poor performance. It is best to use a string connection instead of a double quotation mark string.

  1. BAD:
  2. $ Output="ThisIsAPlainString ";
  3. GOOD:
  4. $ Output='ThisIsAPlainString ';
  5. BAD:
  6. $ Type="Mixed ";
  7. $ Output="ThisIsA$ TypeString ";
  8. GOOD:
  9. $ Type='Mixed ';
  10. $ Output='ThisIsA'.$ Type.'String ';

Reference

PHP code performance optimization 3. Use the echo function to output strings

The echo () function is used to print the result, which is easier to read. In the next example, you can see that the result has better performance.

  1. BAD:
  2. Print ($ myVariable );
  3. GOOD:
  4. Echo$ MyVariable;

Reference

PHP code performance optimization 4. Do not use connectors in echo

Many PHP programmers (including me) Don't know how to use commas to separate multiple variables when outputting them with foul smell, instead of using strings to connect them first, in the first example below, there will be performance problems because the connector is used, because this will require the PHP engine to First connect all the variables and then output them, in the second example, the PHP engine will output them in sequence.

  1. BAD:
  2. Echo'Hello,MyNameIs'.$ FirstName.$ LastName.'AndILiveIn'.$ City;
  3. GOOD:
  4. Echo'Hello,MyNameIs',$ FirstName,$ LastName,'AndILiveIn',$ City;

Reference

PHP code performance optimization 5. Use switch/case instead of if/else

The use of switch/case statements instead of if/else statements for the judgment of only one variable will provide better performance, and the code will be easier to read and maintain.

  1. BAD:
  2. If ($ _ POST ['action']='Add '){
  3. AddUser ();
  4. }Elseif($ _ POST ['action']='Delete '){
  5. DeleteUser ();
  6. }Elseif($ _ POST ['action']='Edit '){
  7. EditUser ();
  8. }Else{
  9. DefaultAction ();
  10. }
  11. GOOD:
  12. Switch ($ _ POST ['action']){
  13. Case'Add ':
  14. AddUser ();
  15. Break;
  16. Case'Delete ':
  17. DeleteUser ();
  18. Break;
  19. Case'Edit ':
  20. EditUser ();
  21. Break;
  22. Default:
  23. DefaultAction ();
  24. Break;
  25. }

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.