A collection of PHP code optimizations

Source: Internet
Author: User
Tags ini pear php code php script sql injection strlen

1. If a method can be static, then declare that he is static, speed can be increased by 1/4;

2.echo is more efficient than print because Echo does not return a value, and print returns an integral type;

3. Set the maximum number of cycles before the loop, not in the loop;

4. Destroy the variable to release the memory, especially the large array;

5. Avoid using magic methods such as __get, __set, __autoload, etc.

6.requiere_once () compared with resource consumption;

7. Using absolute paths in includes and requires

8. If you need to get the time when the script executes, $_server[' Requset_time '] is better than times ();

9. Can use the character processing function, uses them as far as possible, because the efficiency is higher than the regular;

10.str_replace character Substitution Buzheng replaces preg_replace fast, but STRTR is 1/4 faster than str_replace;

11. If a function can accept an array and accept a simple character as a parameter, such as a character substitution, and the argument list is not too long, consider using a few more concise substitution statements, replacing only one character at a time instead of accepting the array as a lookup and substitution parameter. The,1+1>2; of the punches

12. Using @ To cover up the error will reduce the speed of script operation;

$row [' ID '] is 7 times times faster than $row[id], it is recommended to develop the habit of adding quotes to the array key;

14. Error messages are useful;

15. Do not use functions in the loop, such as for ($x =0; $x < count ($array); $x), the count () function is calculated on the outside;

16. The fastest way to establish local variables in a method is almost as fast as calling local variables in a method;

Ofadmin.com

17. The establishment of a global variable is twice times slower than the local variable;

18. Creating an Object attribute (a variable within a class), such as ($this->prop++), is 3 times times slower than a local variable;

19. The establishment of an undeclared local variable is 9-10 times slower than an initialized local variable;

20. Declaring a global variable that has not been used by any one function can also degrade performance (as with declaring the same number of local variables), and PHP may check whether the global variable exists;

21. The performance of the method has nothing to do with the number of methods defined within a class, because I add 10 or more methods to the test class (these methods before and after the test method) have no difference in performance;

22. The performance of the method in subclasses is superior to that in the base class;

23. A function that calls only one parameter and the function body is empty is equal to 7-8 times $localvar++ operation, while a similar method (the function in the class) runs equal to about 15 times $localvar++ operations;

24.Surrounding your string by ' instead of ' would make things interpret a little faster since PHP looks to variables De "..." but not inside ' ... '. Of course you can only does this when you don ' t need to have variables in the string.

25. It is quicker to use commas instead of point segmentation when outputting strings. Note: This only works on Echo, which accepts some strings as arguments;

Ofadmin.com

26. In the Apache server 2nxx a PHP script page than the corresponding HTML static page generation to generate at least 2-10 times more time, it is recommended to use more static HTML pages and a small number of footsteps;

27. Unless your cache is installed, your PHP script will need to be recompiled every time it is accessed. It is recommended that you install a PHP caching program so that you can significantly improve your 20-100% performance by removing some repetitive compilations;

28. It is recommended to use memcached, High-performance distributed Memory object caching system to improve the performance of dynamic network applications and reduce the burden of the database;

29. Use the Ip2long () and LONG2IP () functions to convert the IP address into an integer to be stored in the database instead of the character type. This can reduce storage space by almost 1/4. At the same time, the address can be easily sorted and quickly searched;

30. Use CHECKDNSRR () through the existence of domain name to confirm the validity of part of the email address, this built-in function to ensure that each domain name corresponds to an IP address;

31. If you are using the PHP5 and mysql4.1 versions above, consider using the mysql_* modified function mysqli_*;

32. Try to like using ternary operators (?:);

33. Before you want to thoroughly redo your project, see if Pear has anything you need. Pear is a huge repository of resources that many PHP developers know about;

34. Using Highlight_file () can automatically print a very good formatted source code copy of the page;

35. Use the error_reporting (0) function to prevent potentially sensitive information from being displayed to the user. The ideal error report should be completely disabled in the php.ini file. But if you're using a shared virtual host, php.ini you can't modify, then you'd better add the error_reporting (0) function, put it on the first line of each script file (or load it with require_once ()) This effectively protects sensitive SQL queries and paths from being displayed when errors occur; Ofadmin.com

36. Use Gzcompress () and gzuncompress () to compress (extract) a large-capacity string () when saving (fetching) the database. This built-in function uses the GZIP algorithm to compress to 90%;

37. A function can have multiple return values by reference to the parameter variable address. You can add a "&" in front of the variable to indicate delivery by address rather than by value;

38.Fully Understand "magic quotes" and the dangers of SQL injection. I ' m hoping that most developers reading the are already with SQL familiar. However, I list it here because it's absolutely critical to understand. If you have ' ve never heard the term before, spend the entire rest of the day Googling and reading.

39. Using strlen () because the speed is not too good to invoke some other actions such as lowercase and hash table queries, we can use Isset () to achieve similar functions, isset () speed is better than strlen ();

40.When incrementing or decrementing the value of the variable $i + + happens to be a tad slower then + + $i. This is something PHP specific and does don't apply to other languages, so don ' t go modifying your C or Java code thinking I T ' ll suddenly become faster, it won ' t. + + $i happens to is faster in PHP because instead of the 4 opcodes used for $i + + only need 3. Post incrementation actually causes in the creation of a temporary var this is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization which opcode optimized like Zend ' s PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty O F ISPs and servers running without an opcode optimizer.

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.