PHP performance optimization skills

Source: Internet
Author: User

Google shared some tips, tutorials, and tools such as webpage performance optimization on Google Code's website "let's make the web faster" (making the web faster, today I will translate a technical article: PhP performance optimization skills. I don't know the five skills he mentioned.

1. do not just copy variables. 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!

Bad: $ description = $ _ post ['description']; echo $ description;

Good: Echo $ _ post ['description'];

2. 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. 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. Bad: $ output = "this is a plain string"; good: $ output = 'this is a plain string '; bad: $ type = "mixed"; $ output
= "This is a $ type string"; good: $ type = 'mixed'; $ output = 'this is A'. $ type. 'string ';

3. Use the echo function to output the string. Use the echo () function to print the result, which is easier to read. In the next example, you can see better performance. Bad: Print ($ myvariable); Good: Echo $ myvariable;

4. do not use connectors in Echo. Many PHP programmers (including me) may use commas to separate them when outputting multiple variables with foul smell, instead of using strings to connect them first, in the first example below, performance problems may occur due to the use of connectors, this requires 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. Bad: Echo 'hello, my name is '. $ firstname. $ lastname.' And I live in '. $ city; good: Echo
'Hello, my name is ', $ firstname, $ lastname,' and I live in', $ city;

5. the use of switch/case instead of IF/else for the judgment of only one variable, the use of switch/case statements instead of IF/else statements will have better performance, and the code is easier to read and maintain. Bad: if ($ _ post ['action'] = 'add') {adduser ();} elseif ($ _ post ['action'] = 'delete ') {deleteuser ();} elseif ($ _ post ['action'] = 'edit') {edituser ();} else {
Defaultaction ();} Good: Switch ($ _ post ['action']) {Case 'add': adduser (); break; Case 'delete': deleteuser (); break; Case 'edit': edituser (); break; default: defaultaction (); break ;}

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.