-------> Php code optimization and php problems

Source: Internet
Author: User
Tags flock
------- & 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


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 ", $ 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!

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.