------- & Gt; php code optimization and php problems summary 1. in the function, when passing an array <br/> using return is more efficient than using global <br/> For example <br/> f 1, in the function, when passing an array
Using return is more efficient than using global
For example
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.
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 ", $ Filedata );
$ Filedb = explode (" ", $ 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!
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.