PHP Program Performance Optimization Special Recommendation

Source: Internet
Author: User

1. The difference between single and double quotes

Most PHP developers (including me before) don't care much about their usage, but there's a big difference.

The PHP engine allows the use of single and double quotes to encapsulate string variables, but there is a big difference! A string that uses double quotes is to tell the PHP engine to read the string first, find the variable, and change to the value of the variable. In general, strings are not variable, so using double quotes can lead to poor performance. It is best to use string concatenation instead of double quote strings.

$var = ' This is a test '; This is the canonical notation.

$var = "This is a test $pargam";//is also the canonical notation

$var = "This is a test";/There's nothing wrong with that, but like it said,

2. Do not copy the variables randomly
Sometimes in order to make the PHP code cleaner, some novice PHP (including me) will copy the predefined variables to a shorter name variable, in fact, the result is to increase the amount of memory consumption, only to make the program more slow. Imagine that in the following example, if the user maliciously inserts a 512KB byte of text into the text input box, this will result in 1MB of memory consumption!

Non-canonical wording
$description = $_post[' description '];
Echo $description;

The standard wording
echo $_post[' description '];

3. Use the Echo function to output a string
Using the Echo () function to print results that are easier to read, you can see better performance in the next example.

Print ($myVariable);//nonstandard writing
echo $myVariable//canonical notation

4. Do not use connectors in Echo
A lot of PHP programmers (including me) don't know that when you output multiple variables with echo, you can use commas to separate them without having to connect them first, as in the first example, because you have a performance problem with a connector, because you'll need PHP The engine first connects all the variables, then the output, and in the second example, the PHP engine prints them sequentially.

Echo ' Hello, my name is '. $firstName. $lastName. ' and I live in '. $city; nonstandard consumption performance
Echo ' Hello, my name are ', $firstName, $lastName, ' and I live in ', $city//specification

5. Use Switch/case instead of If/else
For the judgment of only a single variable, the use of switch/case statements instead of IF/ELSE statements has better performance, and the code is easier to read and maintain.

if ($_post[' action '] = = ' Add ') {
AddUser ();
} elseif ($_post[' action '] = = ' Delete ') {
DeleteUser ();
} elseif ($_post[' action '] = = ' edit ') {
Edituser ();
} else {
DefaultAction ();
}

Switch ($_post[' action ']) {
Case ' Add ':
AddUser ();
Break
Case ' Delete ':
DeleteUser ();
Break
Case ' edit ':
Edituser ();
Break
Default
DefaultAction ();
Break
}

6. Don't make unnecessary substitutions.
Even if there is no substitution, str_replace allocates memory for its parameters. It's slow! Solution:

-Use Strpos to find (very fast) to see if replacement is needed, and replace efficiency if needed:
-If you need to replace: the efficiency is almost equal, the difference is about 0.1%.
-If no replacement is needed: Use Strpos fast 200%.

7. Use the @ operator with caution
Do not abuse the @ operator. Although the @ looks simple, there are many actions in the background. Use @ compared to the @, efficiency gap: 3 times times.
In particular, do not use @ in the loop, in 5 cycles of testing, even if the first with error_reporting (0) to turn off the error, after the loop is completed and then open, than the @ faster.

8. Improve PHP file access efficiency
When you need to include other PHP files, use the full path, or the relative path that is easy to convert.


Include ' file.php tutorial '; Bad approach

Incldue './file.php '; Good

Include '/path/to/file.php '; Ideal

?>

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.