Tutorials to .com

Tutorials to .com » Php » Others » php and php code optimization related issues summary

php and php code optimization related issues summary

Print View , by: iSee ,Total views: 7 ,Word Count: 811 ,Date: Sun, 19 Apr 2009 Time: 8:04 PM

1, in the function, passing an array
The use of return to be efficient than the use of global
For example,


function userloginfo ($ usertemp) (
$ detail = explode ("|",$ usertemp);
return $ detail;
)
$ login = userloginfo ($ userdb);

Than

function userloginfo ($ usertemp) (
global $ detail;
$ detail = explode ("|",$ usertemp);
)
userloginfo ($ userdb);



To be efficient

2, (the code used to obtain the corresponding web site directory, recommended)

$ urlarray = explode ('/',$ HTTP_SERVER_VARS [ 'REQUEST_URI']);
$ urlcount = count ($ urlarray); unset ($ urlarray [$ urlcount-1]);
$ ofstarurl = 'http://'. $ HTTP_SERVER_VARS [ 'HTTP_HOST']. implode ('/',$ urlarray);

The code than

$ pre_urlarray = explode ('/',$ HTTP_SERVER_VARS [ 'HTTP_REFERER']);
$ pre_url = array_pop ($ pre_urlarray);

To be efficient

3, in the cycle to determine when the use of value judgments than equivalent high-performance identity
$ a = 2; $ b = 2;
For example,
if ($ a == $ b) $ c = $ a;
Than
if ($ a === $ b) $ C++ = $ a;
Efficient

4, mysql query to make full use of where in the use of limit
Machado limit pre-recorded a few, very fast, but most face a number of queries will be slow
Use in. In a continuous record of inquiry, very fast, non-continuous record of the first run will be slightly slower, but will be followed by faster!

5, NT server data manipulation than the stability of Unix / Linux

6, prior to use to make full use of the output ob_start (); can speed up the output speed, the application of NT or nuli / linux, type of server unlix If you use ob_start ( 'ob_gzhandler'); output efficiency will be higher

7, to determine when to make full use of if ($ a == his value) to make full use of negative time if (empty ($ a)), because this program runs faster

8, when the use of range! = And <> the efficiency of a considerable

9, personal experience was the use of $ a = "11111111111111"; the efficiency and $ a ='11111111111111 '; considerable. Do not like talking about very different books

10, the use of a standardized SQL statement, MySQL will be conducive to the resolution

11, the use of

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);



COOKIE need to refresh to take effect

12, the use of

$ handle = fopen ($ filename, wb);
flock ($ handle, LOCK_SH);
$ filedata = fread ($ handle, filesize ($ filename));
fclose ($ handle);

Than

file ($ filename);

In terms of speed or stability to be excellent

13, truncated string optimization function (could have been 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, process shielding case

for ($ asc = 65; $ asc <= 90; $ asc + +)
(/ / Strtolower () function in a number of servers will be garbled!
if (strrpos ($ regname, chr ($ asc ))!== false)
(
$ error = "user name in order to avoid confusion, the user name in the ban on the use of capital letters, use the lowercase letters";
$ reg_check = 0;
)
)




15, do not use file (); and do not use fget (); (unstable or slow) the array from a function

function openfile ($ filename, $ method = "rb")
(
$ handle = @ fopen ($ filename, $ method);
@ flock ($ handle, LOCK_SH);
@ $ filedata = fread ($ handle, filesize ($ filename));
@ fclose ($ handle);
$ filedata = str_replace ( "", "<ofstar:>", $ filedata);
$ filedb = explode ( "<ofstar:>", $ filedata);
/ / array_pop ($ filedb);
$ count = count ($ filedb);
if ($ filedb [$ count-1 ]=='){ unset ($ filedb [$ count-1]);)
return $ filedb;
)
/ / This function although the code more, but in terms of speed and stability of the great advantages!





php other Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.