1. When passing an array in a function
Using return is more efficient than using 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
2. (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!
5. The data operation stability of the NT Server is less than that of unix/linux
6. Use ob_start () whenever possible before output. It can speed up the output. It is applicable to NT or nuli/linux. If you use ob_start ('ob _ gzhandler') for unlix servers '); higher output efficiency
7. Try to use if ($ a = his/her value) when determining whether to use if (empty ($ a) whenever possible, because the program runs faster.
8. When not supported! = 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. Use
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. Use
$ 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 the string optimization function (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-sensitive 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) Obtain 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 <ofstar:>", $ filedata );
$ Filedb = explode ("<ofstar:>", $ filedata );
// Array_pop ($ filedb );
$ Count = count ($ filedb );
If ($ filedb [$ count-1] = '') {unset ($ filedb [$ count-1]);}
Return $ filedb;
}
// This function has many codes, but it has great advantages in speed and stability!
Write to this
The above is my summary, but the correctness has been tested repeatedly. If you have any questions, please test it first and discuss it again. Thank you!