Sharing PHP technology development skills

Source: Internet
Author: User
This document is intended for PHP programmers who have some experience. it will introduce some development skills in PHP development and hope to inspire readers. 1. improving PHP running efficiency 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 has a high traffic volume, narrow bandwidth, or other factors, <LINKhref = "http: // www. php100

This document is intended for PHP programmers who have some experience. it will introduce some development skills in PHP development and hope to inspire readers.

  1. improve PHP running efficiency

One of the advantages of PHP is that it is fast. it is enough for general website applications. 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.1. code optimization

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:

Procedure 1:
Reset ($ arr );
While (list ($ key, $ value) = each ($ arr )){
Echo "Key: $ key; Value: $ value
N ";
}
Procedure 2:
Foreach ($ arr as $ key => $ value ){
Echo "Key: $ key; Value: $ value
N ";
}

1.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 transmission modes on the server. One is that the page has been compressed in advance, and only the compressed page can be transferred to the client during transmission. this is suitable for scenarios where there are many static webpages, however, 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 and are generated dynamically when the server receives requests from the client user, therefore, each generated dynamic page must be compressed before being uploaded to the client. After PHP version 4.0.4. add a line in the INI file to configure "output_handler = ob_gzhandler", so that each dynamically generated page will be compressed before being transferred to the client. However, according to the instructions on the official site of PHP, this parameter cannot be used with "zlib. the output_compression = 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, and it cannot work 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, which is usually 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:

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 occupation)
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

1.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 immediately returned to the user, instead, the interpreter returns the execution result to the customer after the interpreter is interpreted and executed, during which the database access is usually involved. In this case, a problem occurs. when two users access the same page, the system will perform operations on the two requests respectively, but in fact these two operations may be identical, this 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 and stores the execution results 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. The popular Cache management program is Zend Cache of Zend Technologies.

2. execute system external commands

As a server-side scripting language, PHP is fully competent for tasks such as simple writing 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:

2.1. use special functions provided by PHP

PHP provides three 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 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 ");

 

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.