Introduction: This is a detailed page for improving PHP running efficiency. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 330437 'rolling = 'no'>
[Switch] One of the advantages of PHP is that it is fast. For general website applications, it can be said that it is enough. However, if the site's traffic is high, bandwidth is narrow, or other factors cause a performance bottleneck on the server, you may have to think of other ways to further speed up PHP.
1.CodeOptimization
(1) Replace I = I + 1 with I + = 1. In line with the C/C ++ habits, the efficiency is high.
(2) Use PHP internal functions as much as possible. Before writing a function, check the manual to see if there are any related functions.
(3) try to use a single quotation mark string. The efficiency of a single quotation mark string is higher than that of a double quotation mark string.
(4) use foreach instead of while to traverse the array. When traversing an array, foreach is much more efficient than while loop, and you do not need to call the reset function. The two traversal methods are as follows:
Program1:
Reset ($ ARR );
While (List ($ key, $ value) = each
($ ARR )){
Echo "key: $ key; Value: $ value <br> N ";
}
Procedure 2:
Foreach ($ arr as $ key => $ value ){
Echo "key: $ key; Value: $ value <br> N ";
}
2. compress the page
Http1.1 supports page compression and transmission. That is to say, the server compresses a page and sends it to the client, decompress the page on the client, and then displays it to the customer. There are two types of data transfer on the server side
One method is that the page has been compressed in advance, so you only need to transfer the compressed page to the client during transmission. This method is suitable for scenarios where there are many static webpages, but for most websites, there are many dynamic pages.
This method is not suitable, because many pages that are uploaded to the client do not actually exist. It is generated dynamically when the server receives client user requests, therefore, each generated dynamic page must be packaged before being uploaded to the client.
. After PHP version 4.0.4, you can add a line in the PHP. ini file to configure "output_handler =
Ob_gzhandler ", so that each dynamically generated page will be compressed before it is transferred to the client. However, according to the instructions on the official site of PHP, this parameter cannot match
"Zlib. output_compression =
The on parameter is used at the same time, because it is easy to cause PHP to work abnormally. In addition, it can only compress the dynamically generated pages of the PHP program, especially for a large number of static pages, especially image files. However
The mod_gzip module provides the apahe function to compress a static page before it is sent to the client. The maximum compression ratio is 10.
3. That is to say, the website transmission rate has increased by more than three times. To use mod_gzip and configure Apache, add some parameters to the httpd. conf file.
Quantity:
Mod_gzip_on
Yes (whether the module is effective)
Mod_gzip_minimum_file_size
1002 (minimum compressed file size)
Mod_gzip_maximum_file_size
0 (maximum compressed file size, 0 indicates no limit)
Mod_gzip_maximum_inmem_size
60000 (maximum memory available)
Mod_gzip_item_include
File "..gif 102 Sina> double_quotation (files ending with GIF must be compressed and transmitted)
Mod_gzip_item_include
File ". txt102sina> double_quotation
Mod_gzip_item_include
File ". html102sina> double_quotation
Mod_gzip_item_exclude
File ". css102sina> double_quotation
3. File Cache
This method is usually for CGI programs such as PHP and Perl, because these programs share a common feature that after receiving a user request, the results are not returned to the user immediately,
After the interpreter is interpreted and executed, the execution result is returned to the customer, during which the database access is usually involved. In this case, a problem occurs. When two users access the same page, the system will separately
But in fact these two operations may be the same, which increases the burden on the system. Therefore, the common solution is to create a space in the system memory. When the user visits the page for the first time
The execution result is stored in the memory. when a user visits the page again, the system calls the page directly from the memory without re-interpreting the execution, this memory space is called cache. Currently popular Cache Management
The processing program is Zend cache of Zend technologies.
4. Execute system external commands
As a server-side scripting language, PHP is fully competent for tasks such as writing simple or complex dynamic web pages. But this is not always the case. Sometimes, to implement a function, you must use an external program (or a command) of the operating system to get twice the result with half the effort.
You can use the following three methods to call external commands in PHP.
5. Use special functions provided by PHP
PHP provides three specialized functions for executing external commands: System (), exec (), passthru ().
System ()
Prototype: String
System (string command [, int return_var])
The system () function is similar to the one in other languages. It executes the given command, outputs and returns the result. The second parameter is optional and is used to obtain the status code after the command is executed.
Example:
System ("/usr/local/bin/Webalizer ");
Exec ()
Prototype: String Exec
(String command [, String Array [, int return_var])
Exec () Functions and
Similarly, the system () command also executes the given command, but does not output the result, but returns the last line of the result. Although it only returns the last line of the command result, the second parameter array can be used to obtain
The method is to append the result row by row to the end of array. Therefore, if the array is not empty, we recommend that you use unset () to clear it before calling it. Only when the second parameter is specified,
You can use the third parameter to obtain the status code for command execution.
Example:
Exec ("/bin/ls
-L ");
Exec ("/bin/ls
-L ", $ res );
Exec ("/bin/ls
-L ", $ res, $ RC );
Passthru ()
Prototype: void
Passthru (string command [, int return_var])
Passthru () only calls the command and does not return any knot
But directly output the command running result to the standard output device. Therefore, the passthru () function is often used to call a tool like pbmplus (UNIX for image processing,
Outputs the stream of the original binary image. It can also get the status code of command execution.
Example:
Header ("Content-Type:
Image/GIF ");
Passthru ("./ppmtogif
Hunte. ppm ");
6. Use the popen () function to open the process.
The preceding method can only execute commands, but cannot interact with commands. However, sometimes you must enter something into the command. For example, when adding a Linux system user, you must call Su to change the current user to the root user, the su command must enter the root password on the command line. In this case, it is obviously not feasible to use the method mentioned above.
The popen () function opens a process pipeline to execute the given command and returns a file handle. Since a file handle is returned, you can read and write it. In php3
The handle can only be used in a single operation mode, either write or read. From PhP4, you can read and write at the same time. Unless this handle is opened in a mode (read or write), you must call pclose ()
Function to close it.
Example 1:
$ Fp = popen ("/bin/ls
-L "," R ");
Example 2:
/* Add a system user in PHP
The following is a routine. Add a user named James,
The root password is verygood. For reference only
*/
$ Sucommand = "su -- login Root
-- Command ";
$ Useradd = "useradd ";
$ Rootpasswd = "verygood ";
$ User = "James ";
$ User_add = sprintf ("% s" % s
% S "", $ sucommand, $ useradd, $ user );
$ Fp = @ popen ($ user_add, "W ");
@ Fputs ($ FP, $ rootpasswd );
@ Pclose ($ FP );
Develop a good program Style
In many cases, the most valuable feature of PHP may be that its weakest link is its loose syntax.
PHP can be widely used because it enables many inexperienced web developers to create powerful applications without having to think too much about planning, coherence, and documentation. Unfortunately
Many PHPSource codeVery bloated, difficult to read or even unable to maintain. An important factor determining code maintainability is the code format and comments. All code of a project should be grouped throughout the form
. The following describes how to develop a good program code style in a PHP program.
7. indent
All the code of the developer should be written in indent mode. This is the most basic measure to improve code readability. Even if you do not comment on your code, indentation is very helpful for others to understand your code.
8. Add comments
It is a good habit to add comments during programming. php allows you to add comments to the page code. The specific annotation method is the same as the C language annotation syntax. You can add comments to the script. You can use "/*" and "*/" to comment out a paragraph. You can use the double slash "//" as the annotator.
9. Control Structure
This depends largely on your taste. I can still see that many of the control structure codes do not contain branch statements, resulting in poor readability. If you use if statements without branches, not only will the readability become worse, when others modify your program, many bugs may occur. See the following example:
Bad example:
If ($ A = 1)
Echo 'a was equal to 1 ';
This is very hard to identify. It works normally, but the code is not appreciated by others except you.
Examples of improvements:
If ($ A = 1)
Echo 'a was
Equal to 1 ';
At least this code can be understood, but it still does not have good maintainability. What if I want an additional event to happen when $ A = 1, or if I want to add a branch? If later programmers forget to add the keyword "Large arc" or "else", a bug will occur in the program.
Perfect example
If ($ A =
1) & ($ B = 2 )){
Echo 'a was
Equal to 1'; // other code can be easily added
} Elseif
($ A = 1) & ($ B = 3) {// other operations
}
Note that there is a space after if and elseif. This separates this statement from the function call. In addition, although there are no statements in the elseif Execution Section, only comments exist, on the surface, it seems redundant, but it gives a very convenient prompt to the programmers who want to maintain the program in the future, and it is very helpful to add functions.
10. Use include to implement modular functions
You can store common function functions in a PHP file. When you use the functions in other PHP pages, you can include the PHP file containing the function in the PHP file that calls the function. In this case, you can use the include function. The specific syntax is:
Include ($ filename );
Note:
1. Avoid self-inclusion, that is, file1 contains file1. When multiple files contain statements, avoid indirect self-inclusion, that is, loop inclusion. For example, file1 contains file2 and file2 contains file3, file3 contains file1.
2. The included script language must be of the PHP language type or script statement segment.
Http://www.zxbc.cn/html/phpjq/0908342313525_3.html
More articles on How to Improve PHP Running Efficiency
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/330437.html pageno: 11.