// 好
function userloginfo($usertemp) {
$detail = explode("|", $usertemp);
return $detail;
}
$login = userloginfo($userdb);
// 差
function userloginfo($usertemp) {
global $detail;
$detail = explode("|", $usertemp);
}
userloginfo($userdb);
$urlarray = explode('/', $_SERVER['REQUEST_URI']);
unset($urlarray[count($urlarray)-1]);
$base_url = 'https://' . $_SERVER['HTTP_HOST'] . implode('/', $urlarray);
if ($a === $b) $c = $a; // 型別也相同時更快
LIMIT 100000, 10 需掃描前 10 萬行;WHERE id IN (...) 連續 ID 查詢極快,非連續首次稍慢但有快取加成。
生產環境首選 Linux。
ob_start(); // 通用
ob_start('ob_gzhandler'); // Linux 下啟用 gzip 壓縮更快
效能較好且語意清晰。
任選其一保持專案一致。
實測 $a="..."; 與 $a='...'; 無顯著差異,重點在可讀性與跳脫需求。
大寫關鍵字、明確欄位名、避免 SELECT *。
if ($online) {
$online1 = $online; // 先賦值給變數
setcookie('online1', $online, $cookietime, $ckpath, $ckdomain, $secure);
}
// 直接 setcookie 需重新整理才生效
$handle = fopen($filename, 'rb');
flock($handle, LOCK_SH);
$data = fread($handle, filesize($filename));
fclose($handle);
速度與穩定性雙贏。
function substrs($content, $length) {
if (strlen($content) > $length) {
$num = 0;
for ($i = 0; $i < $length - 3; $i++) {
if (ord($content[$i]) > 127) $num++;
}
$content = ($num % 2 == 1) ? substr($content, 0, $length - 4) : substr($content, 0, $length - 3);
$content .= ' ...';
}
return $content;
}
for ($asc = 65; $asc <= 90; $asc++) {
if (strrpos($regname, chr($asc)) !== false) {
$error = "禁止使用大寫字母";
$reg_check = 0;
}
}
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<ofstar:>", $filedata);
$filedb = explode("<ofstar:>", $filedata);
$count = count($filedb);
if ($filedb[$count-1] == '') unset($filedb[$count-1]);
return $filedb;
}
以上皆為實測驗證經驗,歡迎實測後討論交流。