Five practical tips for optimizing PHP Performance

Source: Internet
Author: User
The following are five optimization skills that will be helpful for development after you have mastered them. 1. Use single quotes for strings. The PHP engine allows single quotes and double quotes to encapsulate string variables. However, this 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. I

The following are five optimization skills that will be helpful for development after you have mastered them. 1. Use single quotes for strings. The PHP engine allows single quotes and double quotes to encapsulate string variables. However, this 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. I

The following are five optimization skills that will be helpful for development after you have mastered them.

1. 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 = “This is a plain string”;
  3. GOOD:
  4. $output = 'This is a plain string';
  5. BAD:
  6. $type = “mixed”;
  7. $output = “This is a $type string”;
  8. GOOD:
  9. $type = 'mixed';
  10. $output = 'This is a ' . $type .' string';

2. 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, the memory will be 1 MB!

 
 
  1. BAD:
  2. $description = $_POST['description'];
  3. echo $description;
  4. GOOD:
  5. echo $_POST['description'];

3. Use the echo function to output strings

The echo () function is used to print the results. In addition to easier reading, you can see better performance in the next example.

 
 
  1. BAD:
  2. print($myVariable);
  3. GOOD:
  4. echo $myVariable;

4. Do not use connectors in echo

Many PHP programmers (including me) do not know that when using echo to output multiple variables, they can be separated by commas instead of strings. 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, my name is' . $firstName . $lastName . ' and I live in ' . $city;
  3. GOOD:
  4. echo 'Hello, my name is' , $firstName , $lastName , ' and I live in ' , $city;

5. replace if/else with switch/case

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

From: chinaitlab

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.