PHP Tips for improving site efficiency _php skills

Source: Internet
Author: User

Programmers always want to make their programs consume the least amount of resources, run faster, and have less code when writing programs. We often lose a lot of things while pursuing these. The goal of optimization is to get the fastest running speed and the easiest code to maintain with the least cost. The following is a summary of several PHP optimizations:
1, the use of In_array
Avoid using In_array () on large arrays, and avoid using this function in loops for arrays that contain more than 20 elements. In_array () can be very resource intensive. This effect may be small for small arrays, but checking for large arrays in one loop can take several seconds. If you do need this feature, use Isset () to find the element of the array. You actually use the key name to query the key value. 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 should always be enclosed in single quotes to avoid confusion. Note that this is a case of using a string instead of using a variable to do a key name.

For example:

Bad writing
$foo = $assoc _array[blah];
Good writing
$foo = $assoc _array[' blah '];
Bad writing
$foo = $assoc _array["$var"];
Good writing
$foo = $assoc _array[$var];

3. String expression
in PHP, there are two different ways to represent a string-single or double quotes. The main difference is that the grammar interpreter replaces the strings represented by the double quotes instead of the strings represented by single quotes. 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 consumption of the program because the parser does not need to process a large number of strings that have no variables at any time.
Similarly, if you use a string variable as an argument in a function call, you do not need to enclose the variable in quotation marks. This can cause the grammar interpreter to do much more than work hard. Remember that almost all the escape characters in double quotes are not valid for single quotes. You need to be aware of the above rules, but sometimes for the readability of your code, you can make an exception.

For example:

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

4, the operation of the cycle definition
in the comparison section if there is an operation, be sure to optimize it. Because this part will operate at each 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 a value for this invariant amount at the beginning of the loop for
 ($i = 0, $size = sizeof ($post _data); $i < $size; $i + +)
 {
  do_something ()
 } 

5, branch judgment, switchcase better than IfElse
using branching statements is much better than using a lot of ifelse.

6, do not abuse the @ operator
Don't abuse the @ operator, it looks simple, but it's actually a few times less efficient than @ without @.
In particular, do not use @ in loops.

7, the method of acquiring time
A lot of times we get used to getting the current time (), but it's a function, and it's better to just use $_server[' request_time '.

8. String length judgment
two kinds of writing:
(1) if (strlen ($STR) < 5) {/* do something */}
(2) if (Isset ($str [5])) {/* do something */}

The above is a small series of online collation of the PHP optimization skills, hope to help you learn!

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.