Five programming habits for PHP Performance Optimization-PHP source code

Source: Internet
Author: User
After searching for programming optimization on the Internet, we will see a lot of articles, but they are all the same content, next, I will sort out the five most useful programming habits for PHP performance optimization and share them with you. I hope the article will help you. After searching for programming optimization on the Internet, we will see a lot of articles, but they are all the same content, next, I will sort out the five most useful programming habits for PHP performance optimization and share them with you. I hope the article will help you.

Script ec (2); script

1. Do not copy variables at will.
Sometimes, in order to make PHP code more clean, some new PHP users (including those who follow the rules themselves) 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!

The Code is as follows:
Bad habits:
$ Description = $ _ POST ['description'];
Echo $ description;
Can be written:
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! 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.

The Code is as follows:
Bad habits:
$ Output = "This is a plain string ";
Can be written:
$ Output = 'this is a plain string ';
Bad habits:
$ Type = "mixed ";
$ Output = "This is a $ type string ";
Can be written:
$ 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 habits:

The Code is as follows:
Print ($ myVariable );
Can be written:
Echo $ myVariable;

4. Do not use connectors in echo
Many PHP programmers do not know that when using echo to output multiple variables, they can use commas instead of 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.

The Code is as follows:
Bad habits:
Echo 'hello, my name is '. $ firstName. $ lastName.' and I live in '. $ city;
Can be written:
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 habits:

The Code is as follows:
If ($ _ POST ['action'] = 'add '){
AddUser ();
} Elseif ($ _ POST ['action'] = 'delete '){
DeleteUser ();
} Elseif ($ _ POST ['action'] = 'edit '){
EditUser ();
} Else {
DefaultAction ();
}
Can be written:
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.