Php tips for improving website efficiency

Source: Internet
Author: User
With the rise of PHP development in China, code optimization has become a common topic. From time to time, the author will go to the CSDN forum to discuss and communicate with others, and there will be PHP development and optimization problems. this is also a way to quickly improve himself. When programmers write programs, they always want to minimize the resource occupation of their programs, run faster, and have less code. We often lose a lot of things while pursuing these things. The goal of optimization is to get the fastest running speed and the most easily maintained code at the lowest cost. Below is a summary of some PHP optimizations:
1. in_array usage
Avoid using in_array () on large arrays, and avoid using this function for arrays containing more than 20 elements in a loop. In_array () consumes a lot of resources. This may have little impact on small arrays, but it may take several seconds to check large arrays in a loop. If you need this function, use isset () to find the array elements. In fact, the key name is used 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 a string without single quotes as an array key name, but we do not want this to happen-the key name should always be included in single quotes to avoid confusion. Note that a string is used instead of a variable as the key name.

For example:

// Bad syntax $ foo = $ assoc_array [blah]; // good syntax $ foo = $ assoc_array ['blah']; // bad syntax $ foo = $ assoc_array ["$ var"]; // good syntax $ foo = $ assoc_array [$ var];

3. string expression
In PHP, two different methods are used to represent a string-single quotation marks or double quotation marks. The main difference is that the syntax interpreter will replace variables for strings represented by double quotation marks, rather than strings represented by single quotation marks. Therefore, you should always use single quotes unless you really need to process variables in strings. In this way, we can reduce the program running consumption, because the syntax interpreter does not need to process a large number of strings with no variables at all each time.
Similarly, if you use a string variable as a parameter in a function call, you do not need to include this variable in quotation marks. This will cause the syntax interpreter to do a lot of useless work. Remember, almost all escape characters in double quotes are invalid for single quotes. You need to pay attention to the above rules, but sometimes you can make exceptions for the readability of the code.

For example:

// Bad syntax $ str = "This is a really long string with no variables for the parser to find. "; do_stuff (" $ str "); // write $ str = 'This is a really long string with no variables for the parser to find. '; do_stuff ($ str); // sometimes the single quotes are not so appropriate $ post_url = $ phpbb_root_path. 'Posting. '. $ phpEx. '? Mode = '. $ mode.' & start = '. $ start; // double quotation marks can sometimes make code lines more concentrated $ post_url = "{$ phpbb_root_path} posting. $ phpEx? Mode = $ mode & start = $ start ";

4. operations in loop definition
If computation exists in the comparison section, you must optimize it. This section performs operations in every step of the loop:

// In each loop, the sizeof function must be called for ($ I = 0; $ I <sizeof ($ post_data); $ I ++) {do_something ();} // you can assign the constant value for ($ I = 0, $ size = sizeof ($ post_data); $ I <$ size; $ I ++) {do_something ();}

5. branch judgment. switchcase is better than ifelse
Using branch statements is much better than using many ifelse statements.

6. do not abuse the @ operator
Do not abuse the @ operator. it looks very simple, but it is actually several times less efficient to use @ than to use.
Do not use @ in loops @.

7. how to get the time
We often get the current time using time (), but TIME () is always a function. it is better to use $ _ SERVER ['request _ time'] directly.

8. string length determination
Two methods:
(1) if (strlen ($ str) <5) {/* do something *

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.