PHP code optimization Tips 40 Less detours _php tutorial

Source: Internet
Author: User
1. If a method can be static, then declare that he is static, the speed can be increased by 1/4;
2.echo is more efficient than print, because Echo has no return value, and print returns an integer 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 like __get, __set, __autoload, etc.;
6.requiere_once () comparative consumption of resources;
7. Use absolute paths in includes and requires, so that less time is spent on the analysis path;
8. If you need to sexinsex to the time of the script execution, $_server[' requset_time ' is better than times ();
9. Can use character processing function, as far as possible with them, because the efficiency is higher than the regular;
10.str_replace character substitution singular replace preg_replace fast, but STRTR is 1/4 faster than str_replace;
11. If a function can accept both an array and a simple character as a parameter, such as a character substitution, and the argument list is not too long, consider using more concise substitution statements, replacing only one character at a time instead of accepting the array as the find and replace parameter. Punches,1+1>2;
12. Masking errors with @ will slow down the script;
$row [' ID '] is 7 times times faster than $row[id], it is recommended to form the habit of array key quotes;
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 first outside;
16. The fastest way to establish local variables in a method is to make 97xxoo almost as fast as a local variable in a method;
17. Creating a global variable is twice times slower than local variables;
18. Create an object property (the variable inside the class) for example ($this->prop++) is 3 times times slower than local variables;
19. Creating an undeclared local variable is 9-10 times slower than an initialized local variable;
20. Declaring a global variable that is not used by any of the functions will 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 is not related to the number of methods defined in a class, because I add 10 or more methods to the test class (these methods before and after the test method) performance is no different;
22. The performance of the method in the subclass is better than that in the base class;
23. Only one parameter is called and the function that is empty is equal to 7-8 $localvar++ operations, and a similar method (function in class) runs equal to approximately 15 $localvar++ operations;
24.Surrounding your string by ' instead of ' would make things interpret a little faster since PHP looks for variables Insi De "..." but not inside ' ... '. Of course you can be only if you don't need to having variables in the string.
25. Using commas instead of point segmentation is faster when outputting strings. Note: This only works for Echo, which can accept some strings as arguments;
26. In the Apache server, a PHP script page than the corresponding HTML static page generated 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 each time it is accessed. It is recommended to install a PHP cache program, so that by removing some repetitive compilation to significantly improve the performance of your 20-100%;
28. It is recommended to use memcached, high-performance distributed memory object cache system to improve dynamic network application performance and reduce the burden of database;
29. Use the Ip2long () and LONG2IP () functions to convert the IP address into an integer type into the database instead of the character type. This reduces storage space by almost 1/4. At the same time, the address can be easily sorted and quickly find;
30. Use CHECKDNSRR () to confirm the validity of some email address through the existence of the domain name, this built-in function can ensure that each domain name corresponds to an IP address;
31. If you are using PHP5 and mysql4.1 versions above, consider using the mysql_* modified function mysqli_*;
32. Try to enjoy using the ternary operator (? :);
33. Before you want to thoroughly re-do your project, see if Pear has anything you need. Pear is a huge repository of resources that many PHP developers know;
34. Use Highlight_file () to automatically print a well-formatted copy of the page source code;
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. However, if you are using a shared virtual host, php.ini you can not modify, then you'd better add error_reporting (0) function, put in the first line of each script file (or with require_once () To load) This can effectively protect sensitive SQL queries and paths from being displayed when errors occur;
36. Use Gzcompress () and gzuncompress () to compress (decompress) the large-capacity string when the database is stored in (out). This built-in function can be compressed to 90% using the GZIP algorithm;
37. Make a function have multiple return values by referring to the argument variable address. You can add a "&" to the variable before it is passed by address rather than by value;
38.Fully Understand "magic quotes" and the dangers of SQL injection. I ' m hoping that most developers reading this is already familiar with SQL injection. However, I list it here because it's absolutely critical to understand. If you've never heard the term before, spend the entire rest of the day Googling and reading.
39. Use strlen () because you want to invoke some other operations such as lowercase and hash table query so the speed is not too good, we can use Isset () to achieve similar functions, isset () faster than strlen ();
40.When incrementing or decrementing the value of the variable $i + + happens to bes 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 being faster in PHP because instead of 4 opcodes used for $i + + you only need 3. Post incrementation actually causes in the creation of a temporary var that's then incremented. While pre-incrementation increases the original value directly. This is one of the optimization so opcode optimized like Zend ' s PHP optimizer. It's 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.

http://www.bkjia.com/PHPjc/364563.html www.bkjia.com true http://www.bkjia.com/PHPjc/364563.html techarticle 1. If a method can be static, then declare that he is static, the speed can be increased by 1/4; 2.echo is more efficient than print, because Echo has no return value, and print returns an integer type; 3. Before the loop is set ...

  • 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.