Summary of PHP Optimizations

Source: Internet
Author: User
Tags switch case

Today saw the next phpbb of the relevant norms, feel that there are a lot of places worth learning.

Here's a summary of some of the optimizations for PHP:

1, the use of In_array

Avoid using In_array () on large arrays, and avoid using this function for arrays containing more than 20 elements in a loop. In_array () can consume resources very much. This effect may be small for small arrays, but it may take several seconds for a large array to be checked in a loop. If you do need this feature, use Isset () to find the array element. The key value is actually queried using the key name. Calling Isset ($array [$var]) is much faster than In_array ($var, Array_keys ($array)).

2. Array Key Name

In PHP, it is legal to use strings that are not enclosed in single quotes as array key names, but we do not want to--the key names should always be enclosed by single quotes to avoid confusion. Note that this is the case of using a string instead of using a variable to do the key name. For example:

Bad wording.
$foo = $assoc _array[blah];
Good wording.
$foo = $assoc _array[' blah ');
Bad wording.
$foo = $assoc _array["$var"];
Good wording.
$foo = $assoc _array[$var];

3. String expression

In PHP, there are two different ways to represent a string-single or double quotation marks. The main difference is that the syntax interpreter replaces the string represented by double quotation marks with the string that is represented by single quotation marks. So you should always use single quotes, unless you really need to handle variables in a string. In this way, we can reduce the running cost of the program because the syntax interpreter does not need to process a large number of strings at all without a variable at a time.

Similarly, if you use a string variable as a parameter in a function call, you do not need to enclose the variable in quotation marks. This causes the grammar interpreter to do a lot of work without working hard. Remember that almost all escape characters in double quotes are not valid for single quotes. You need to be aware of the above rules, but sometimes it is appropriate to make exceptions for the readability of your code. For example:

Bad wording.
$str = "A really long string with no variables for the parser to find.";
Do_stuff ("$str");
Good wording.
$str = ' This was a really long string with no variables for the parser to find. ';
Do_stuff ($STR);
Sometimes the single quotation marks are not so suitable
$post _url = $phpbb _root_path. ' Posting. ' $phpEx. '? mode= '. $mode. ' &start= '. $start;
Double quotes can sometimes make lines of code more focused
$post _url = "{$phpbb _root_path}posting. $phpEx mode= $mode &start= $start";

4, the operation of the loop definition

In the comparison section if there is an operation, be sure to optimize it. Because this part will operate at every step in the loop:

In each loop, the sizeof function is called
for ($i = 0; $i < sizeof ($post _data); $i + +)
{
Do_something ();
}
You can assign this constant amount to the starting part of the loop
for ($i = 0, $size = sizeof ($post _data); $i < $size; $i + +)
{
Do_something ();
}


5, branch judgment, switch case better than if else

It is much better to use a branching statement than with many if else.

6. Do not misuse the @ operator

Do not abuse the @ operator, it looks simple, but in fact, with @ than the efficiency of the @ is several times worse.

In particular, do not use @ in loops.

7. How to get the time

Many times we are accustomed to using time () to get the current moment, but it is a function, rather than just using $_server[' request_time '.

8, string length judgment

Two types of notation:

(1) if (strlen ($STR) < 5) {/* do something */}

(2) if (Isset ($str [5])) {/* do something */}

The second type of

is better than the first.

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.