40 Small instances of optimized PHP code

Source: Internet
Author: User
Tags pear php script

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 has no return value, Print returns an integer type;3.set the maximum number of cycles before the loop, not in the loop;4.destroying variables to release memory, especially large arrays;5. Avoid using like __get, __set,__autoload and other magic methods;6.requiere_once () comparative consumption of resources;7.use absolute paths in includes and requires, which takes less time to parse the path;8. If you need to sexinsex to the time the script executes,$_server[' Requset_time '] Better than Time ();9.If you can use character processing functions, use them as much as possible, because efficiency is higher than regular;10.str_replace character replacement singular replaces 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 @ can slow down the script;13.$row[' ID '] than$row[id] speed 7 times times faster, it is recommended to form an array of key quotation marks habit;14.error messages are useful;15. Do not use functions in the loop, such as for ($x= 0;$x<Count($array);$x),Count() function is calculated first outside;16.Local variables are the fastest in the method, and 97xxoo is almost as fast as calling local variables in the method .17.Building a global variable is twice times slower than local variables;18. Create an object property (a variable inside the class) for example ($this->prop++) is 3 times times slower than local variables;19. Creating an undeclared local variable is 9 slower than an initialized local variable-10 times times;20.declaring a global variable that is not used by any of the functions also degrades performance (as with declaring the same number of local variables), and PHP may check for the existence of the global variable;21st.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 (the methods before and after the test method) have no difference in performance;22.The performance of the method in subclasses is better than that in the base class;23. Call only one parameter and the function body is empty the time spent is equal to 7-8 times$localvar+ + operations, and a similar method (a function in a class) runs equal to approximately 15 times$localvar++operation;24.Surrounding yourstringBy ' instead of ' would make things interpret a little faster since PHP looks forVariables inside "..." but not inside ' ... '. Of course can only DoThis if you don't need to having variables in thestring.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. A PHP script page in the Apache server will take at least 2 more than the corresponding HTML static page generation10 times times, 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 you can obviously improve your 20-100% by removing some repetitive compilations's performance;28.It is suggested 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 can almost reduce the 1/4 of storage space. At the same time, the address can be easily sorted and quickly find;30.use CHECKDNSRR () through the existence of the domain name to confirm the validity of some email address, 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 completely 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 copy of a well-formatted 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. But if you're using a shared virtual host, php.ini you can't modify, then you'd better add error_reporting (0) function, which is placed on the first line of each script file (or loaded with require_once ()), which effectively protects sensitive SQL queries and paths from being displayed when errors occur;36. Usegzcompress() and gzuncompress () compress (unzip) 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 "&" in front of the variable"to represent 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 this is already familiar with SQL injection. However, IListIt here because it's absolutely critical to understand.IfYou'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 action such as Lowerc ASE 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. ++$ihappens to being faster in PHP because instead of 4 opcodes used for $i+ + you are only need 3. Post incrementation actually causes in the creation of a temporaryvarThat's then incremented. whilePre-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.

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.