PHP Performance Optimization method

Source: Internet
Author: User
Tags case statement

Five practical tips for optimizing PHP performance:
    
The following are five optimization techniques, which are very helpful for development after mastery.
    
1. Use single quotation marks for strings
    
The PHP engine allows the use of single and double quotes to encapsulate string variables, but this is a big difference! A string using double quotes tells the PHP engine to first read the string contents, look for the variable in it, and change the value to the variable. Generally speaking, strings are not variable, so using double quotes can cause poor performance. It is best to use a string connection instead of a double-quoted 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 ';
    
2. Do not copy variables randomly
    
sometimes in order to make the PHP code more neat, some novice PHP (including me) will copy the predefined variables into a shorter name of the variable, in fact, the result is an increase in memory consumption, will only make the program more slowly. Imagine, in the following example, if the user maliciously inserted 512KB bytes of text into the text input box, which will result in 1MB of memory consumption!
    
Bad :
    
$description = $_post[' description ');
    
echo $description;
    
Good:
    
echo $_post[' description '];
    
3. Use the Echo function to output a string
    
using the Echo () function to print out the results is easier to read, and in the next example you can see better performance.
    
Bad :
    
print ($myVariable);
    
Good:
    
echo $myVariable;
    
4. Do not use connectors in Echo
    
very ***php programmers (including me) do not know when using the stench to output multiple variables, you can actually use commas to separate, instead of using strings to first connect them, as in the first example below, because the use of connectors will have a performance problem, because this will require PHP The engine first connects all the variables, then the output, and in the second example, the PHP engine outputs them sequentially.
    
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. Use switch/case instead of If/else
    
for a single variable, using the Switch/case statement instead of the If/else statement 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 ;
    
    }

PHP Performance Optimization method

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.