php代碼優(yōu)化及php相關(guān)問題總結(jié) 問題|優(yōu)化" />

亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊 ------->php代碼優(yōu)化及php相關(guān)問題總結(jié)

------->php代碼優(yōu)化及php相關(guān)問題總結(jié)

Jun 21, 2016 am 09:15 AM
content handle online quot

問題|優(yōu)化

1,在函數(shù)中,傳遞數(shù)組時
使用 return 比使用 global 要高效
比如


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



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



要高效

2,(這個代碼用于得到程序目錄對應(yīng)的網(wǎng)址,推薦使用)

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

這段代碼比

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

要高效

3,在循環(huán)中判斷時,數(shù)值判斷使用恒等要比等于高效
$a=2;$b=2;
比如
if($a==$b)$c=$a;

if($a===$b)$c=$a;
高效

4,mysql 查詢時盡量使用where in 少用 limit
limit查多記錄的前幾條, 速度很快, 但是查詢最面幾條就會慢
使用in .在查詢連續(xù)性記錄,非??? 非連續(xù)性記錄第一次運行會稍微慢一點,但是之后將比較快!

5,NT服務(wù)器數(shù)據(jù)操作穩(wěn)定性不及unix/linux

6,輸出前使用盡量使用 ob_start(); 可以加快輸出速度,適用NT或nuli/linux,對unlix類服務(wù)器 如果使用 ob_start('ob_gzhandler');輸出效率將更高

7,判斷的時候盡量使用if($a==他的值) 否定的時候盡量使用if(empty($a)),因為這樣程序運行更快速

8,使用不等時 != 與 效率相當(dāng)

9,個人經(jīng)驗得 使用 $a="11111111111111"; 的效率和 $a='11111111111111'; 相當(dāng).并不象書本說的相差很大

10,使用規(guī)范的SQL語句, 會有利于MySQL的解析

11,使用

if($online){
$online1=$online;
setcookie('online1',$online,$cookietime,$ckpath,$ckdomain,$secure);
}



COOKIE將馬上生效
使用

if($online)
setcookie('online1',$online,$cookietime,$ckpath,$ckdomain,$secure);



COOKIE需要再刷新一次才能生效

12,使用

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



file($filename);

無論在速度還是穩(wěn)定上都要優(yōu)秀

13,截斷字符串優(yōu)化函數(shù)(可避免?字符出現(xiàn))

function substrs($content,$length) {
if(strlen($content)>$length){
$num=0;
for($i=0;$iif(ord($content[$i])>127)$num++;
}
$num%2==1 ? $content=substr($content,0,$length-4):$content=substr($content,0,$length-3);
$content.=' ...';
}
return $content;
}



比如$newarray[1]=substrs($newarray[1],25);

14,程序中屏蔽大小寫

for ($asc=65;$asc{ //strtolower() 此函數(shù)在一些服務(wù)器會產(chǎn)生亂碼!
if (strrpos($regname,chr($asc))!==false)
{
$error="為了避免用戶名混亂,用戶名中禁止使用大寫字母,請使用小寫字母";
$reg_check=0;
}
}




15,不使用 file();和不使用 fget();(不穩(wěn)定或速度慢) 取一數(shù)組函數(shù)

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;
}
//這個函數(shù)雖然代碼比較多,不過在速度和穩(wěn)定性上優(yōu)勢很大!



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出,該如何解決 php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出,該如何解決 Jun 13, 2016 am 10:23 AM

php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出php提交表單通過后,彈出的對話框怎樣在當(dāng)前頁彈出而不是在空白頁彈出?想實現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗證用PHP在后端,那么就用Ajax;僅供參考:HTML code

Valve issues GTA V refunds after anti-cheat strands Steam Deck gamers Valve issues GTA V refunds after anti-cheat strands Steam Deck gamers Sep 20, 2024 pm 06:18 PM

Rockstar Games recently added BattleEye anti-cheat to Grand Theft Auto V, seemingly deliberately breaking Linux support in the process, leaving Steam Deck owners out in the cold, and placing the blame for Steam Deck incompatibility squarely on the sh

圖片消失怎么解決 圖片消失怎么解決 Apr 07, 2024 pm 03:02 PM

圖片消失如何解決先是圖片文件上傳$file=$_FILES['userfile']; ?if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

CSS content properties explained: content, counter, and quotes CSS content properties explained: content, counter, and quotes Oct 21, 2023 am 10:16 AM

Detailed explanation of CSS content attributes: content, counter and quotesCSS (cascading style sheets) is an integral part of front-end development. It can help us beautify web pages and enhance user experience. In CSS, there are some special properties that can be used to control the display of text content, including content, counter, and quotes. This article explains these properties in detail and provides specific code examples. 1. content attribute content attribute

不用數(shù)據(jù)庫來實現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 不用數(shù)據(jù)庫來實現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒有關(guān)問題 Jun 13, 2016 am 10:15 AM

不用數(shù)據(jù)庫來實現(xiàn)用戶的簡單的下載,代碼如下,但是卻不能下載,請高手找下原因,文件路勁什么的沒問題。

圖片消失怎么解決 圖片消失怎么解決 Jun 13, 2016 am 10:09 AM

圖片消失如何解決先是圖片文件上傳$file=$_FILES['userfile']; ?if(is_uploaded_file($file['tmp_name'])){$query=mysql_query("INSERT INTO gdb_banner(image_src ) VALUES ('images/{$file['name'

為什么小弟我在php上寫的這個代碼,在瀏覽器上什么都不顯示 為什么小弟我在php上寫的這個代碼,在瀏覽器上什么都不顯示 Jun 13, 2016 am 10:24 AM

為什么我在php上寫的這個代碼,在瀏覽器上什么都不顯示啊

Microsoft AI CEO says using open web content to train AI models isn\'t wrong Microsoft AI CEO says using open web content to train AI models isn\'t wrong Jun 30, 2024 pm 10:59 PM

In an interview with CNBC's Andrew Ross Sorkin, Microsoft AI CEO Mustafa Suleymanexpressed his understanding of web copyright law. Suleyman says that content that is accessible on the open web is "freeware," and anyone can copy it, reproduc

See all articles