50 tips to improve the efficiency of PHP execution (i)

Source: Internet
Author: User

In the project development process, often encountered some PHP processing program performance under the situation, the program runs in the Centos+nginx environment, although this has a lot of reasons such as: the server itself configuration, operating environment Nginx service, PHP-FPM configuration, etc. There is still a little more to phper that the program is not better Optimized.

First class, variable type:
1) mainly uses the undefined variable, but directly uses the variable, when does the program development end, the programmer likes to turn off the debug, lets all notice and the warning all shadow hides, but essentially PHP even if closes the debug also will record the log, All the notice and warning information is written in the log file, nothing more than a waste of performance, many phper have this little habit, including Me.
2) in addition, in the program should try to pay attention to the use of variables, such as the best use of single quotation marks, rather than double quotation marks, after all, double quotation marks are parsed into single quotation marks in the Execution.
3) when it comes to the index of the key-value in the array, you need to use single quotation marks, and the execution of the program is very inefficient without writing quotation marks.
Second class, function type:
1) session variable, We all know call session This global variable, often we put the value after the session, go straight down to write their own code, But forget the session variable will be in the page after the completion of the execution of the value will be placed in the session for other pages to use, so that if the page execution time is too long, other pages also call this session will wait for the writing session of the page after the completion of the execution of other programs later , but our program does not want this effect, because we only need to write the value to the session after execution, it has been placed in the session, rather than waiting for the page to complete, from the PHP website can see the session variable does have this problem, So we need to shut down the write session after the session is written, plus the session_write_close () function to reduce the idle time waiting for the page to Load. 2) file_get_contents () function, This function means to get the content of the page of the remote url, but although this says, many people will not notice such a problem "timeout", if not get the page content, the program will always be stuck here, Many people will contact the Settings page timeout or set the Max_execution_time maximum execution timeout in the php.ini file, but if you are using PHP-FPM (that is, php-cgi), The parameter will be considered INVALID. It is necessary to set the maximum execution time-out in the PHP-FPM configuration file, which ultimately does not work and needs to be addressed, and still requires us to add a timeout to the call to the function in order to fundamentally resolve the Problem.
Third class, Reference file type:
Referencing a file a lot of the time we used to use include, but there are also some tips for using it, If you are familiar with the framework development program, there will be "include" type of folder under Project Project. The program finds the included files in the order that they are found in the Include typeface folder under the current working directory root path, and then in the Include typeface folder where the file is currently Located. That is, the use of this include improper, the program will follow this way under the root path to find, This is also a very confusing thing, so you should try to use the file in the most easy to find the location, so that it is conducive to the execution of the program Efficiency. There are also functions like echo outputting multiple strings or variables, using "." Connection efficiency is less efficient than "," connection Execution.

50 tips to improve PHP execution efficiency (i)

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.