53 points to improve the efficiency of PHP programming

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags apache array calls class code data echo file

It would be faster to enclose the string in single quotes instead of double quotes. Because PHP searches for variables in double-quoted strings, single quotes do not. Note that only echo can do this: it's a "function" that takes multiple strings as arguments, Echo is a language structure, not a real function, so the function with double quotes).

1, if you can define the class method as static, as far as the definition of static, it will speed up nearly 4 times.

2, $ row ['id'] is 7 times faster than $ row [id].

3, echo faster than the print, and the use of echo multiple parameters (Annotation: refers to a comma instead of a period) instead of string concatenation, such as echo $ str1, $ str2.

4, before the implementation of the for loop to determine the maximum number of cycles, do not calculate the maximum value of each cycle, it is best to use foreach instead.

5, write off those who do not have variables, especially large arrays, in order to release memory.

6, try to avoid using __get, __ set, __ autoload.

Require_once () is expensive.

8, include files as far as possible to use the absolute path, because it avoids PHP to include_path to find the file speed, analytical operating system path will take less time.

9, Use $ _SERVER ['REQUEST_TIME'] is better than time () if you want to know when the script started executing.

10, the function instead of regular expressions to complete the same function.

The str_replace function is faster than the preg_replace function, but the strtr function is four times more efficient than the str_replace function.

12, if a string substitution function can accept an array or character as a parameter, and the parameter length is not long, you can consider the extra write a replacement code, so that each pass parameter is a character, rather than just write a line of code to accept the array As a query and replace parameters.

13, the use of choice branch statement (Annotation: the switch case) is better than using multiple if, else if statements.

14, use @ to mask the wrong message is very inefficient, extremely inefficient.

15, open apache mod_deflate module, you can improve the browsing speed of the page.

16, the database connection should be switched off when used, do not use long connections.

17, the error message is expensive.

18, in the method of increasing local variables, the speed is the fastest. Almost as fast as invoking local variables in functions.

19, increasing a global variable than increasing a local variable 2 times slower.

Increasing an object property (eg: $ this-> prop ++) is three times slower than incrementing a local variable.

21, incrementing an undefined local variable is 9 to 10 times slower than incrementing a predefined local variable.

22, Defining only a local variable without calling it in a function will also slow down (to the extent of incrementing a local variable). PHP probably checks to see if there are global variables.

23, the method call seems to have nothing to do with the number of methods defined in the class because I added 10 methods (both before and after the test method), but no change in performance.

24, derived class methods run faster than the same method defined in the base class.

25, calling an empty function with a parameter, the time it takes to perform the equivalent of 7 to 8 times the local variable increment operation. Similar method calls take nearly 15 times as many local variable increments.

26, Apache parsed a PHP script than parsing a static HTML page 2 to 10 times slower. Try to use static HTML pages, use less script.

27, unless the script can be cached, or each call will recompile time. Introducing a set of PHP caching mechanisms typically improves performance by 25% to 100% to avoid compilation overhead.

28, try to do the cache, you can use memcached. memcached is a high performance memory object caching system that can be used to speed up dynamic web applications and reduce database load. It is useful to cache OP code so that scripts do not have to be recompiled for each request.

When you manipulate a string and need to verify that its length satisfies a certain requirement, you will of course use the strlen () function. This function executes fairly quickly because it does not make any calculations and only returns the known string length stored in the zval structure (C's built-in data structure for storing PHP variables). However, since strlen () is a function, there is more or less some slowness because function calls go through a number of steps, such as lowercase letters (hash: lowercase, PHP does not distinguish between case and function), hash lookups, Will be executed along with the function being called. In some cases, you can use the isset () trick to speed up your code execution.

(For example below)

if (strlen ($ foo) <5) {echo "Foo is too short" $$}

(Compare with the following tips)

if (! isset ($ foo {5})) {echo "Foo is too short" $$}

The call to isset () happens to be faster than strlen (), because unlike the latter, isset () as a language structure means that its implementation does not require function lookups and lowercase letters. That is, you do not actually spend too much overhead checking the length of the string.

34, $ i ++ will be slower than ++ $ i when executing the increment or decrement of variable $ i. This difference is PHP specific and does not apply to other languages, so do not change your C or Java code and expect them to get faster and useless immediately. ++ $ i is faster because it requires only 3 opcodes and $ i ++ requires 4 instructions. Post increment actually produces a temporary variable, which is then incremented. The front increases directly in the original value increases. This is one of the best practices, as Zend's PHP optimizer did. Keep in mind that this optimization is a good idea, because not all instruction optimizers do the same optimization, and there are a large number of Internet service providers (ISPs) and servers that do not have an instruction optimizer.

35, not necessarily object-oriented (OOP), object-oriented is often costly, each method and object calls will consume a lot of memory.

36, not to use the class to achieve all the data structures, the array is also useful.

37, do not subdivide the method too much, think about what you really want to reuse the code?

When you need it, you can always break the code down into methods.

39, try to use a lot of PHP built-in functions.

If you have a lot of time-consuming functions in your code, you can consider implementing them in C extensions.

41, assess the profile of your code. The checkers tell you what parts of the code have been consumed. The Xdebug debugger includes a verification program, which can show the bottleneck of the code as a whole.

42, mod_zip acts as an Apache module to compress your data on the fly and reduce data transfer by 80%.

43, in the file_get_contents alternative file, fopen, feof, fgets and other series of methods, try to use file_get_contents because he is much more efficient! But pay attention to file_get_contents open a URL file when the PHP version of the problem;

44, as little as possible for file operations, although PHP file operation efficiency is not low;

45, optimize the Select SQL statement, as far as possible in the case of Insert, Update operation (update, I was criticized);

46, as much as possible the use of PHP internal functions (but I was in order to find a PHP does not exist in the function, a waste of time can write a custom function, experience problems!);

47, inside the loop do not declare variables, especially the large variables: object (which seems to be not only PHP which should pay attention to the problem?);

48, multi-dimensional array try not to loop nested assignment;

49, PHP can be used within the internal string manipulation functions, do not use regular expressions;

50, foreach more efficient, try to use foreach instead of while and for loop;

51, single quotes instead of double quotes string;

52, "Use i + = 1 instead of i = i + 1. Accord with the habit of c / c ++, the efficiency is still high";

53, on the global variable, it should be used unset () off;

Related Article

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.