Share PHP Technology Development Tips _php Tutorial

Source: Internet
Author: User
The reader of this document is an experienced PHP programmer, the document will introduce some development techniques in PHP development, hoping to have some inspiration to the reader.

  1. Improve the efficiency of PHP operation

One of the advantages of PHP is that it is fast and is sufficient for general website applications. However, if your site has high traffic, narrow, or other factors that make the server performance bottleneck, you may have to think of other ways to further improve the speed of PHP.

1.1. Code optimization

1, replace i=i+1 with I+=1. In line with C + + habits, efficiency is high.

2. Use PHP intrinsics as much as possible. Before you write your own function, consult the manual to see if there are any related functions or thankless.

3. Use single-quote strings as far as possible with single quotation marks. Single-quote strings are more efficient than double-quoted strings.

4. Use foreach instead of while to iterate through the array. When iterating through an array, foreach is significantly more efficient than the while loop and does not require a call to the Reset function. The two methods of traversal are as follows:

Program 1:
  
Reset ($arr);
  
while ( List ($ Key , $value) = each ($arr)) {
  
Echo " Key: $key; Value: $value
N
" ;
}
Program 2:
  
foreach ($arr as $ Key = $value) {
  
Echo " Key: $key; Value: $value
N
" ;
}

1.2. Compress the page

The HTTP1.1 protocol supports page compression transfer, which means that the server transmits a page compression to the client and then the page is decompressed and displayed to the client. There are two kinds of transmission on the server side, one is the page has been compressed in advance, as long as the compressed page is transferred to the client on the line, this applies to static Web pages more, but for most sites, dynamic pages more, this method is not suitable, because many of the pages to the client is not actually, Is the server receives the client user request to generate dynamically, so requires each to generate a dynamic page before uploading to the client before packaging compression. from PHP 4.0.4, you can add a line to the php.ini file to configure "Output_handler = Ob_gzhandler" so that each dynamically generated page will be compressed before it is delivered to the client, but according to the PHP official site instructions, This parameter can not be used in conjunction with the "zlib.output_compression = on" parameter, because it is easy to cause PHP to work abnormally, and it can only compress the PHP program dynamically generated pages, for a large number of static pages, especially image files. However, the Mod_gzip module provides Apahe with the ability to compress static pages before being passed on to the client, which can be compressed up to 10, typically up to 3, which means that the transmission rate of the website is increased by more than three times times. To use Mod_gzip also to configure Apache appropriately, you need to include some parameters in the httpd.conf file:

mod_gzip_on Yes (module is active)
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 memory consumption)
Mod_gzip_item_include
file " .. Gif102sina>double_quotation (the file ending in GIF to compress the transfer)
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 approach is usually for CGI programs such as PHP, Perl, because these programs have a common feature is that the user's request is not immediately returned to the user, but the interpreter after the execution of the implementation of the results returned to the customer, which usually involves the database access. This will cause a problem, when two users access the same page, the system will operate on two requests, but in fact the two operations may be identical, so the invisible increases the burden of the system. Therefore, the usual solution is to open up a space in the system memory, when the user first access the page after the execution of the results in the memory, when the user once again access to the page, the system will be directly from memory to recall the page without the need to re-interpret the execution, this memory space is called the cache. The current popular cache management program is Zend Technologies Company's Zend Cache.

2. Execute system external commands

PHP as a server-side scripting language, like writing simple, or complex dynamic Web pages such a task, it is fully capable. But this is not always the case, sometimes in order to achieve a function, you have to rely on the operating system external programs (or called commands), so you can do more with less.

Calling external commands in PHP can be implemented in three ways:

2.1. Special functions provided by PHP

PHP provides a total of 3 specialized functions for executing external commands: System (), exec (), PassThru ().

System ()

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

The system () function is similar in other languages, it executes the given command, outputting and returning the result. The second parameter is optional and is used to get the status code after the command executes.

Example:

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

http://www.bkjia.com/ phpjc/508424.html www.bkjia.com true http://www.bkjia.com/phpjc/ 508424.html techarticle This document is a reader of the experience of PHP programmers, the document will introduce some of the development techniques in PHP development, hoping to have some inspiration to the reader. 1. Improve PHP operation efficiency ...

  • 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.