The following are some of the problems found by phper in actual writing. many other code needs to be optimized and many programs can be accelerated. below is the actual compilation of phper, some of the problems found. many other code needs to be optimized, and many programs can be accelerated.
1. in a function, it is more efficient to use return when passing an array than to use global. for example: Function userloginfo ($ usertemp ){
$ Detail = explode ("|", $ usertemp );
Return $ detail;
}
$ Login = userloginfo ($ userdb );
Ratio
Function userloginfo ($ usertemp ){
Global $ detail;
$ Detail = explode ("|", $ usertemp );
}
Userloginfo ($ userdb );
Efficient
II. (this code is used to obtain the URL corresponding to the program directory, which is recommended) $ Urlarray = explode ('/', $ HTTP_SERVER_VARS ['request _ URI ']);
$ Urlcount = count ($ urlarray); unset ($ urlarray [$ urlcount-1]);
$ Ofstarurl = 'http: // '. $ HTTP_SERVER_VARS ['http _ host']. implode ('/', $ urlarray );
Code comparison
$ Pre_urlarray = explode ('/', $ HTTP_SERVER_VARS ['http _ referer']);
$ Pre_url = array_pop ($ pre_urlarray );
Efficient
3. when judging in a loop, it is more efficient to use constant to determine the value. $ A = 2; $ B = 2;
For example
If ($ a ==$ B) $ c = $;
Ratio
If ($ a ===$ B) $ c = $;
Efficient
4. use where in to use less limit for MySQL queries Limit queries the first few records of multiple records, which is fast, but the query speed is slow
Using in. the query of the continuity record is very fast. the first running of the non-continuity record will be a little slower, but it will be faster later!
V. NT Server data operation stability is less than unix/Linux 6. use ob_start () whenever possible before output (); It can accelerate the output speed and is applicable to NT or nuli/linux. for unlix-type servers, if ob_start ('OB _ gzhandler') is used, the output efficiency will be higher.
7. try to use if ($ a = his value) when judging) If (empty ($ a) is used whenever possible, because the program runs faster.
8. usage time! = Equivalent to <> efficiency 9. the efficiency of using $ a = "11111111111111" in personal experience is quite different from that in $ a = '000000 '.
10. using standard SQL statements will facilitate MySQL parsing 11. usage If ($ online ){
$ Online1 = $ online;
Setcookie ('online1', $ online, $ cookietime, $ ckpath, $ ckdomain, $ secure );
}
COOKIE will take effect immediately
Use
If ($ online)
Setcookie ('online1', $ online, $ cookietime, $ ckpath, $ ckdomain, $ secure );
The COOKIE must be refreshed again to take effect.
12. usage $ Handle = fopen ($ filename, wb );
Flock ($ handle, LOCK_SH );
$ Filedata = fread ($ handle, filesize ($ filename ));
Fclose ($ handle );
Ratio
File ($ filename );
Excellent in both speed and stability
13. truncation of string optimization functions (can be avoided? Characters) Function substrs ($ content, $ length ){
If (strlen ($ content)> $ length ){
$ Num = 0;
For ($ I = 0; $ I <$ length-3; $ I ++ ){
If (ord ($ content [$ I])> 127) $ num ++;
}
$ Num % 2 = 1? $ Content = substr ($ content, 0, $ length-4): $ content = substr ($ content, 0, $ length-3 );
$ Content. = '';
}
Return $ content;
}
For example, $ newarray [1] = substrs ($ newarray [1], 25 );
14. case-insensitive shielding in the program For ($ asc = 65; $ asc <= 90; $ asC ++)
{// Strtolower () this function generates garbled characters on some servers!
If (strrpos ($ regname, chr ($ asc ))! = False)
{
$ Error = "to avoid user name confusion, uppercase letters are not allowed in the user name. please use lowercase letters ";
$ Reg_check = 0;
}
}
15. do not use file (); or do not use fget (); (unstable or slow) take an array function Function openfile ($ filename, $ method = "rb ")
{
$ Handle = @ fopen ($ filename, $ method );
@ Flock ($ handle, LOCK_SH );
@ $ Filedata = fread ($ handle, filesize ($ filename ));
@ Fclose ($ handle );
$ Filedata = str_replace ("\ n", "\ n", $ filedata );
$ Filedb = explode ("", $ filedata );
// Array_pop ($ filedb );
$ Count = count ($ filedb );
If ($ filedb [$ count-1] = '') {unset ($ filedb [$ count-1]);}
Return $ filedb;
}
Part 2: 1. when file_get_contents can be used to replace file, fopen, feof, fgets, and other methods, try to use file_get_contents, because its efficiency is much higher! Note the PHP version of file_get_contents when opening a URL file;
2. perform as few file operations as possible, although PHP file operations are not efficient;
3. optimize Select SQL statements and perform Insert and Update operations as little as possible (I have been maliciously approved in update );
4. try to use PHP internal functions as much as possible (but in order to find a function that does not exist in PHP, it is a waste of time to write a user-defined function, experience problems !);
5. do not declare variables inside the loop, especially big variables: objects (doesn't it seem to be a concern in PHP ?);
6. do not use nested values for multi-dimensional arrays;
7. do not use regular expressions when you can use PHP internal strings to operate functions;
8. foreach is more efficient. use foreach instead of the while and for loops;
9. use single quotes instead of double quotes to reference strings;
10. "replace I = I + 1 with I + = 1. In line with the c/c ++ habits, high efficiency ";
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.