Home  >  Article  >  Backend Development  >  Share PHP technology development skills_PHP tutorial

Share PHP technology development skills_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:35:00744browse

This document is intended for PHP programmers with certain experience. The document will introduce some development techniques in PHP development, hoping to inspire readers.

 1. Improve the operating efficiency of PHP

One of the advantages of PHP is that it is very fast, which can be said to be sufficient for general website applications. However, if the site has high traffic, narrow bandwidth, or other factors that cause the server to have a performance bottleneck, you may have to think of other ways to further increase the speed of PHP.

 1.1. Code Optimization

1. Use i+=1 instead of i=i+1. It conforms to the habits of c/c++ and is highly efficient.

 2. Use PHP internal functions as much as possible. Before writing a function yourself, you should check the manual in detail to see if there are any related functions, otherwise it will be a thankless task.

3. If you can use single-quoted strings, try to use single-quoted strings. Single quoted strings are more efficient than double quoted strings.

4. Use foreach instead of while to traverse the array. When traversing the array, the efficiency of foreach is significantly higher than that of the while loop, and there is no need to call the reset function. The two traversal methods are as follows:

Program 1:
reset ($arr);
while (list($key, $ value) = each ($arr)) {
echo "Key: $key; Value: $value
n
";
} 
Program 2:
foreach ($arr askey = > $value) {
echo "Key: $key; Value: $value
n
";
}

 1.2. Compressed page

HTTP1.1 protocol supports page compression transmission, which means that the server compresses and transmits a page to the client, and then decompresses the page on the client and displays it to the client. There are two transmission methods on the server side. One is that the page has been compressed in advance. When transmitting, you only need to transmit the compressed page to the client. This is suitable for situations where there are many static web pages, but for most sites, dynamic pages are more Many, this method is not suitable, because many pages transmitted to the client actually do not exist. They are generated dynamically by the server upon receiving user requests from the client. Therefore, it is required that each dynamic page generated must be first generated before being transmitted to the client. Packed and compressed. From PHP version 4.0.4 onwards, you can add a line of configuration "output_handler = ob_gzhandler" in the php.ini file, so that each dynamically generated page will be compressed before being transmitted to the client, but according to the instructions on the PHP official site, This parameter cannot be used at the same time as the "zlib.output_compression = on" parameter, because it can easily cause PHP to work abnormally. In addition, it can only compress dynamically generated pages of the PHP program, but it will not work for a large number of static pages, especially image files. However, the mod_gzip module provides Apahe with the function of compressing static pages before transmitting them to the client. Its compression ratio can reach a maximum of 10, and generally can reach 3, which means that the transmission rate of the website has been increased by more than three times. . To use mod_gzip, you also need to configure Apache accordingly. You need to add some parameters to the httpd.conf file:

mod_gzip_on Yes (whether the module is in effect)
mod_gzip_minimum_file_size
1002 (minimum compressed file size)
mod_gzip_maximum_file_size
0 (maximum compressed file size, 0 means no limit)
mod_gzip_maximum_inmem_size
60000 (maximum occupied memory)
mod_gzip_item_include
file "..gif102SINA>DOUBLE_QUOTATION (Files ending in 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

 1.3. File caching

This method is usually used for CGI programs such as PHP and PERL, because these programs have a common feature that after receiving the user's request, the result is not returned to the user immediately, but after interpretation and execution by the interpreter. Returning the execution results to the client usually involves database access. This will cause a problem. When two users access the same page, the system will operate on the two requests separately, but in fact the two operations may be exactly the same, which invisibly increases the burden on the system. Therefore, the usual solution is to open up a space in the system memory. When the user accesses the page for the first time, the execution result is stored in the memory. When the user accesses the page again, the system directly removes the page from the memory. Called out without reinterpretation and execution, this memory space is called cache. A currently popular cache management program is Zend Cache from Zend Technologies.

2. Execute external system commands

PHP, as a server-side scripting language, is fully capable of tasks such as writing simple or complex dynamic web pages. But this is not always the case. Sometimes in order to implement a certain function, you must resort to the external program (or call it command) of the operating system, so that you can get twice the result with half the effort.

To call external commands in PHP, you can use the following three methods:

 2.1. Use the special functions provided by PHP

PHP provides a total of 3 specialized functions for executing external commands: system(), exec(), and passthru().

system()

Prototype: string system (string command [, int return_var])

The system() function is similar to that in other languages. It executes the given command, outputs and returns the result. The second parameter is optional and is used to get the status code after the command is executed.

Example:

system("/usr/local/bin/webalizer/webalizer&q

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508424.htmlTechArticleThis document is intended for PHP programmers with certain experience. The document will introduce some aspects of PHP development. Development skills, I hope it can inspire readers. 1. Improve the operating efficiency of PHP...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn