How does PHP performance be optimized for all?

Source: Internet
Author: User

Performance is the site is a good operation of the key factors, the performance and efficiency of the site affect the company's operating costs and long-term development, the production of high-quality and efficient code is our every developer must have the quality, but also our good professional quality.

How to optimize PHP performance?

One, variable(important)

1), variable size, note that variable size is the most effective means of saving memory, the data from the user form, database and file cache need to control the size of variables. Because the CPU is processing data from memory
2), the variable validity period, using the unset () function to log off unnecessary variables is a good habit, and some unnecessary variables are immediately logged out to improve memory utilization.
3), copy the variable, try not to copy the variable, otherwise it will bring 1 time times the memory consumption, even if the copy variable should be immediately unregister the original variable.
4), variable type, initialize variable note its variable type, and a variable is preferable to only one type state during execution. For an array variable, initialize the declaration as follows: $a = array ();
5), temporary variables, is the temporary storage processing business logic, these are the need to consume memory. If the temporary variable is used, log off immediately, especially in the execution of some process code, for some functions, if the business is very complex, you also need to unregister the temporary variable immediately
6), static variables, for some variables that need to be generated by complex business, if the execution of the program is generated and used multiple times, you can consider using static variables to reduce the number of CPU executions of the program
7), performance of variables: Local variables > Global variables > class properties > Undefined variables.

Second, the circulation(important)

1), minimize the number of cycles.
2), to minimize the cycle of the flight of the level, not more than three layers.
3), avoid excessive business logic within the loop.
4), do not loop include files
5), do not loop through the database operations.
6), priority to use foreach, it is more efficient than for/while
7), do not put count/strlen/sizeof into the for loop in the conditional statement for ($i =0, $count =count ($array); $i < $count; $i + +) {} Do not use for ($i =0; $i < Count ($array); $i + +) {};
8), for ($i = $total; $i >0; $i –) {} performance is better than for ($i =0; $i < $total; $–) {}
9), keep the business logic clear in the circulation body

Three, function (important)

1), function clear, a function to do only one thing, do not knead excessive business logic
2), function code body not more than 20 lines, on the contrary, consider splitting.
3), priority to use PHP built-in functions
4), constants and functions to do one thing at the same time, use constants first.
For example:
· Phpversion () < php_version
· Get_class () < __class__
· Is_null () < NULL = = =
5), echo performance is better than print, when entering multiple variables with echo $str, $str 1, No. Connector
6), $_server[request_time] replace time ();
7), String substitution strtr ()->str_replace ()->preg_replace ()->epreg ();
8), play trim maximum effect, replace substr. $filepath =trim ($filename, '/'). ' /’;
9), Isset/empty Although the function of the two functions are different, but in the same case, it is recommended to use empty ()
10), isfile/file_exist two functions of different functions, file_exist can not only determine whether the file exists, you can also determine whether the directory exists, in the same case, it is recommended to use Is_file




Iv. Documents (important)

1), reduce the number of files contained, reduce disk IO
2), use the full path, or the relative path that is easy to convert. Avoid finding in include_path
3), no more than 2000 lines of code in the file
4), require_once/include_once efficiency is less than require/include, need additional to see if the system has been called this file. Because their calls in a opcode cache are very slow
5), program execution files with requie/require_once, cache files with Include/include_once. Include efficiency better than require
6), optimize the file automatic loading mechanism in SPL, can be relied on yii
7), class library file loading, whether to consider whether the class has been instantiated, you can consider the design pattern of a single case mode
8), the concurrency of file read and write




v. Object-oriented (important)

1), control the number of instances created
2), priority to use constants, class constants
3), precedence for static variables, static properties
4), the structure of the class is reasonable
5), face-like interface programming
6), Package change point
7), relies on abstraction, does not depend on the details
8), priority use of static members
9), the interface of the class is clear and stable, the function of class is single, the communication of class and class is reasonable
10), the benefits of using constants compile-time resolution, no additional cost hash table is smaller, so the internal lookup faster class constants only exist in a specific "namespace", so the hash name shorter code cleaner, make debugging more convenient




Six, string

1), use single quotation marks instead of double quotation marks to refer to strings; avoid retrieving variables in a string

Vii. operations

1), replace i=i+1 with I+=1. In line with C + + habits, high efficiency
2), + + $i more efficient than $i++,– $i [/hide]

Eight, array

1), multidimensional arrays try not to loop nested assignment;
2), use $array[' name '] mode to access the array, prohibit $array[name]/$array ["Name"]

Nine, Judge (important)

1), logical judgment please use switch in a preferred way, for more business logic situation please select If/else, improve the readability of the Code
2), try to control the number of If/else judgment, if too much please consider functional optimization or code optimization
3), as far as possible to use the identity for comparative judgment, the same efficiency than equals, but also to avoid some types of casting errors
4), If/else and _&&amp, a single statement of judgment please choose the form of &&, && efficiency is higher than If/else, as follows:
if ($a = = 1) {
$b = 2;
}
Selectable for ($a = = 1) && $b = 2;




X. Cache

1), using PHP accelerator, buffer opcode
2), example with Memcache/nosql
3), using the In-memory database,
4), using file caching
5), buffering function

Xi. Other

1), less with the @ symbol, seriously affect performance
2), timely shut down remote resources such as databases, FTP, sockets, etc., timely cleanup of these resources

12. Database Optimization (important)

1), reasonable business needs
2), Database schema optimization
3), vertical and horizontal sub-database sub-table
4), index optimization, query optimization
5), third party open source Search tool (Sphinx)
6), the use of the master-slave database server.
For more information, see the-mysql PHP public video, "major website optimization core technology"




13. Front-End optimization

1), Reasonable HTML structure
2), reasonable HTML and CSS at the same time, consider the CSS design reasonable, reduce the HTTP request
3), reasonable HTML and Java script at the same time, consider whether the split is reasonable, reduce the HTTP request
4), optimize Java script code, let the user have a good experience
5), according to the HTTP protocol, optimize high concurrent requests

14. Performance Testing Tools (important)

As long as in Baidu search "xxx How to check site performance" can.
1), Web Server
2), AB
3), Http_load
4), PHP
5), APD
6), Xdebug
7), Mysql
8), explain
9), profiler this article reproduced to Preach Wisdom podcast: HTTP://BBS.ITCAST.CN/THREAD-82934-1-1.HTML?XHW

How does PHP performance be optimized for all?

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.