PHP efficient writing

Source: Internet
Author: User
Tags autoload switch case

0. Use single quotes instead of double quotes to include strings
. Because PHP will search for variables in strings enclosed by double quotes, but not in single quotes. Note: Only ECHO can do this, it is a "function" that can treat multiple strings as parameters (ECHO is a language structure, not a real function, so double quotation marks are added to the function ).
1. If you can define a class method as static, try to define it as static.
It will increase the speed by nearly four times.

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

3. Echo is faster than print
And use multiple echo parameters instead of periods to replace string connections, such as Echo $ str1, $ str2.

4. It is best to use foreach to execute a loop.
Before executing the for loop, determine the maximum number of cycles. Do not calculate the maximum value for each loop.

5. unregister unnecessary variables, especially large arrays.
To release the memory.

6. Avoid using _ Get ,__ set ,__ autoload whenever possible
. For functions starting with _, they are named Magic functions. These functions are all in the kitchen under specific conditions. In general, there are several magic Functions

_ Construct () ,__ destruct () ,__ get () ,__ set () ,__ unset () ,__ call () ,__ callstatic (), __sleep () ,__ wakeup () ,__ tostring () ,__ set_state () ,__ clone () ,__ autoload ()

7. require_once () is expensive
. Commonly used methods to avoid require/include

8. Use absolute paths whenever possible when you include files.
Because it avoids PHP's speed of searching for files in destde_path, it takes less time to parse the operating system path.

9. If you want to know the time when the script starts to be executed (I .e. when the server receives a client request), use $ _ server ['request _ time'] instead of time ().

10. functions use the same functions instead of regular expressions
.

11. 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 replacement function can take an array or character as a parameter and the parameter length is not too long, you can consider writing an additional replacement code so that each parameter passing is a character, instead of writing only one line of code to accept arrays as query and replacement parameters.

13. Use the select branch statement instead of the Condition Statement
The branch Statement (that is, switch case) is better than multiple if and else if statements.

14. No @
, Blocking error messages with @ is very inefficient and extremely inefficient.

15. Open the mod_deflate module of Apache to speed up web page browsing.
.

16. database connection should be switched off when use is complete. Do not use persistent connection.
.

17. Expensive error messages
.

18. Increment local variables in the method at the fastest speed
. It is almost the same as calling a local variable in a function.

19. Increasing a global variable is twice slower than increasing a local variable.
.

20. Incrementing an object property (for example, $ this-> prop ++) is three times slower than incrementing a local variable.

21. Increasing an unspecified local variable is 9 to 10 times slower than increasing a predefined local variable.

22. Defining only one local variable without calling it in the function also slows down (to the extent that it is equivalent to increasing a local variable ). PHP will probably check whether global variables exist.

23. Method calling seems to have nothing to do with the number of methods defined in the class, because I have added 10 methods before and after the test method, but the performance has not changed.

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

25. Calling an empty function with a parameter takes seven to eight times to increment local variables. Similar method calls take nearly 15 times to increment local variables.

26. Apache parses a PHP script two to ten times slower than parsing a static HTML page. Use static html pages and less scripts as much as possible.

27. Unless the script can be cached, it will be re-compiled every time it is called. The introduction of a PHP cache mechanism can generally improve the performance by 25% to 100%, so as to avoid compilation overhead.

28. Use memcached as much as possible. Memcached is a high-performance memory object cache system that can accelerate dynamic web applications and reduce database load. It is useful for the cache of OP code, so that the script does not have to re-compile each request.

29. When operating a string and checking whether its length meets certain requirements, you will use the strlen () function. This function is executed quite quickly because it does not perform any calculations and only returns
Back to zval
The known String Length stored in the structure (C's built-in data structure for storing PHP variables. However, as strlen () is a function, it is more or less slow, because the function call will go through many steps.
For example, if the name of a function is lower-case, PHP does not distinguish the name of the function from upper-case, and hash searches are executed with the called function. In some cases, you can use isset ()
TIPS: Accelerate your code execution.

(For example)
If (strlen ($ Foo) <5) {echo "foo is too short" $}
(Compare with the following tips)
If (! Isset ($ Foo {5}) {echo "foo is too short" $}

Calling isset () happens to be faster than strlen (), because unlike the latter, isset () is used as a language structure, this means that function search and lowercase letters are not required for execution. That is to say, in fact, you do not spend too much money in the top-level code that checks the string length.

34. When the execution variable $ I increments or decreases, $ I ++ is slower than ++ $ I. This difference is exclusive to PhP and does not apply to other languages. Therefore, do not modify your C or Java
Code and expect them to get faster immediately, useless. ++ $ I is faster because it only requires three commands (Opcodes), and $ I ++ requires four commands. An incremental post will actually generate a temporary change
The temporary variable is then incremented. The pre-increment directly increases on the original value. This is a kind of optimization, as the Zend PHP optimizer does. It is good to remember this optimization process.
Because not all command optimizers perform the same optimization and there are a large number of Internet service providers (ISPs) and servers without command optimizers.

35. Object-oriented (OOP) is not required. Object-oriented usually has a high overhead, and each method and object call consumes a lot of memory.

36. arrays are also useful instead of using classes to implement all data structures.

37. Do not subdivide the methods too much. Think carefully about the code you actually intend to reuse?

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

39. Try to use a large number of PHP built-in functions.

40. If there are a large number of time-consuming functions in the code, you can consider using C extension to implement them.

41. Profile your code. The validator will tell you how much time the Code consumes. The xdebug debugger contains an Inspection Program, which can display the code bottleneck in general.

42. mod_zip can be used as an Apache module to instantly compress your data and reduce the data transmission volume by 80%.

43. Use file_get_contents instead of file, fopen, feof, fgets, and other methods.
File_get_contents is much more efficient! Note the PHP version of file_get_contents when opening a URL file;

44. Perform as few file operations as possible, although PHP file operations are not efficient;

45. Optimize select SQL statements and perform insert and update operations as little as possible (I have been maliciously approved in update );

46. Try to use PHP internal functions as much as possible (however, in order to find a function that does not exist in PHP, it is a waste of time to write a user-defined function. This is an empirical problem !);

47. Do not declare variables inside the loop, especially big variables: Objects (Doesn't this seem to be a concern in PHP ?);

48. Do not use nested values for multi-dimensional arrays;

49. Do not use regular expressions when using internal PHP Strings To operate functions;

50. foreach is more efficient. Use foreach instead of the while and for loops;

51. Use single quotes instead of double quotes to reference strings;

52. Replace I = I + 1 with I + = 1. In line with the C/C ++ habits, high efficiency ";

53. For global variables, unset () should be used up;

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.