Can PHP be optimized like this ??

Source: Internet
Author: User

Google made "let's make the web" in Google Code
Faster "(making the web faster) websites share some tips, tutorials, and tools such as web page performance optimization. Today I will translate a tip article: PHP
I don't know the performance optimization skills he mentioned.

1. Do not copy variables at will.

Sometimes to make PHP code more clean, some PHP
New users (including me) will assign predefined variables to a variable with a shorter name. In fact, this will increase the memory consumption by a factor, it will only make the program slower. Imagine the following example:
If the 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, but there is a big difference! Use double quotation marks to tell PHP
The engine first reads the string content, searches for the variable, and changes 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
It is 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 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.

Bad:

Print ($ myvariable );

Good:

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, performance problems occur because the connector is used.
The PHP engine needs to connect all the variables first 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. 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.

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.