|
1,在函数中,传递数组时 cnKLinux联盟 使用 return 比使用 global 要高效 cnKLinux联盟 比如 cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 function userloginfo($usertemp){ cnKLinux联盟 $detail=explode("|",$usertemp); cnKLinux联盟 return $detail; cnKLinux联盟 } cnKLinux联盟 $login=userloginfo($userdb); cnKLinux联盟 cnKLinux联盟 比 cnKLinux联盟 cnKLinux联盟 function userloginfo($usertemp){ cnKLinux联盟 global $detail; cnKLinux联盟 $detail=explode("|",$usertemp); cnKLinux联盟 } cnKLinux联盟 userloginfo($userdb); cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 要高效 cnKLinux联盟 cnKLinux联盟 2,(这个代码用于得到程序目录对应的网址,推荐使用) cnKLinux联盟 cnKLinux联盟 $urlarray=explode('/',$HTTP_SERVER_VARS['REQUEST_URI']); cnKLinux联盟 $urlcount=count($urlarray);unset($urlarray[$urlcount-1]); cnKLinux联盟 $ofstarurl='http://'.$HTTP_SERVER_VARS['HTTP_HOST'].implode('/',$urlarray); cnKLinux联盟 cnKLinux联盟 这段代码比 cnKLinux联盟 cnKLinux联盟 $pre_urlarray=explode('/',$HTTP_SERVER_VARS['HTTP_REFERER']); cnKLinux联盟 $pre_url=array_pop($pre_urlarray); cnKLinux联盟 cnKLinux联盟 要高效 cnKLinux联盟 cnKLinux联盟 3,在循环中判断时,数值判断使用恒等要比等于高效 cnKLinux联盟 $a=2;$b=2; cnKLinux联盟 比如 cnKLinux联盟 if($a==$b)$c=$a; cnKLinux联盟 比 cnKLinux联盟 if($a===$b)$c=$a; cnKLinux联盟 高效 cnKLinux联盟 cnKLinux联盟 4,mysql 查询时尽量使用where in 少用 limit cnKLinux联盟 limit查多记录的前几条, 速度很快, 但是查询最面几条就会慢 cnKLinux联盟 使用in .在查询连续性记录,非常快, 非连续性记录第一次运行会稍微慢一点,但是之后将比较快! cnKLinux联盟 cnKLinux联盟 5,NT服务器数据操作稳定性不及unix/Linux cnKLinux联盟 cnKLinux联盟 6,输出前使用尽量使用 ob_start(); 可以加快输出速度,适用NT或nuli/linux,对unlix类服务器 如果使用 ob_start('ob_gzhandler');输出效率将更高 cnKLinux联盟 cnKLinux联盟 7,判断的时候尽量使用if($a==他的值) 否定的时候尽量使用if(empty($a)),因为这样程序运行更快速 cnKLinux联盟 cnKLinux联盟 8,使用不等时 != 与 <> 效率相当 cnKLinux联盟 cnKLinux联盟 9,个人经验得 使用 $a="11111111111111"; 的效率和 $a='11111111111111'; 相当.并不象书本说的相差很大 cnKLinux联盟 cnKLinux联盟 10,使用规范的SQL语句, 会有利于MySQL的解析 cnKLinux联盟 cnKLinux联盟 11,使用 cnKLinux联盟 cnKLinux联盟 if($online){ cnKLinux联盟 $online1=$online; cnKLinux联盟 setcookie('online1',$online,$cookietime,$ckpath,$ckdomain,$secure); cnKLinux联盟 } cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 COOKIE将马上生效 cnKLinux联盟 使用 cnKLinux联盟 cnKLinux联盟 if($online) cnKLinux联盟 setcookie('online1',$online,$cookietime,$ckpath,$ckdomain,$secure); cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 COOKIE需要再刷新一次才能生效 cnKLinux联盟 cnKLinux联盟 12,使用 cnKLinux联盟 cnKLinux联盟 $handle=fopen($filename,wb); cnKLinux联盟 flock($handle,LOCK_SH); cnKLinux联盟 $filedata=fread($handle,filesize($filename)); cnKLinux联盟 fclose($handle); cnKLinux联盟 cnKLinux联盟 比 cnKLinux联盟 cnKLinux联盟 file($filename); cnKLinux联盟 cnKLinux联盟 无论在速度还是稳定上都要优秀 cnKLinux联盟 cnKLinux联盟 13,截断字符串优化函数(可避免?字符出现) cnKLinux联盟 cnKLinux联盟 function substrs($content,$length) { cnKLinux联盟 if(strlen($content)>$length){ cnKLinux联盟 $num=0; cnKLinux联盟 for($i=0;$i<$length-3;$i++) { cnKLinux联盟 if(ord($content[$i])>127)$num++; cnKLinux联盟 } cnKLinux联盟 $num%2==1 ? $content=substr($content,0,$length-4):$content=substr($content,0,$length-3); cnKLinux联盟 $content.=' ...'; cnKLinux联盟 } cnKLinux联盟 return $content; cnKLinux联盟 } cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 比如$newarray[1]=substrs($newarray[1],25); cnKLinux联盟 cnKLinux联盟 14,程序中屏蔽大小写 cnKLinux联盟 cnKLinux联盟 for ($asc=65;$asc<=90;$asc++) cnKLinux联盟 { //strtolower() 此函数在一些服务器会产生乱码! cnKLinux联盟 if (strrpos($regname,chr($asc))!==false) cnKLinux联盟 { cnKLinux联盟 $error="为了避免用户名混乱,用户名中禁止使用大写字母,请使用小写字母"; cnKLinux联盟 $reg_check=0; cnKLinux联盟 } cnKLinux联盟 } cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 15,不使用 file();和不使用 fget();(不稳定或速度慢) 取一数组函数 cnKLinux联盟 cnKLinux联盟 function openfile($filename,$method="rb") cnKLinux联盟 { cnKLinux联盟 $handle=@fopen($filename,$method); cnKLinux联盟 @flock($handle,LOCK_SH); cnKLinux联盟 @$filedata=fread($handle,filesize($filename)); cnKLinux联盟 @fclose($handle); cnKLinux联盟 $filedata=str_replace("\n","\n<ofstar:>",$filedata); cnKLinux联盟 $filedb=explode("<ofstar:>",$filedata); cnKLinux联盟 //array_pop($filedb); cnKLinux联盟 $count=count($filedb); cnKLinux联盟 if($filedb[$count-1]==''){unset($filedb[$count-1]);} cnKLinux联盟 return $filedb; cnKLinux联盟 } cnKLinux联盟 //这个函数虽然代码比较多,不过在速度和稳定性上优势很大! cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 cnKLinux联盟 先写到这 cnKLinux联盟 以上完全个人的一点小结,,不过正确性都经过反复测试,如果有朋友质疑,请先测试,再讨论,谢谢 cnKLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|