1,使用絕對路徑,方便代碼的遷移:
define( ROOT , pathinfo(__FILE__, PATHINFO_DIRNAME)); require_once(ROOT . ../../lib/some_html' target='_blank'>class.php * PATHINFO_DIRNAME 只返回 dirname * PATHINFO_BASENAME 只返回 basename * PATHINFO_EXTENSION 只返回 extension
2,不要直接使用 require, include, includeonce, requiredonce
$path = ROOT . /lib/ . $class_name . .php require_once( $path );* if(file_exists($path)){ require_once( $path ); }
3,為應用保留調試代碼
在開發環境中, 我們打印數據庫查詢語句, 轉存有問題的變量值, 而一旦問題解決, 我們注釋或刪除它們. 然而更好的做法是保留調試代碼。在開發環境中, 你可以:* define( ENVIRONMENT , development if(! $db- query( $query ) if(ENVIRONMENT == development ) echo $query failed else { echo Database error. Please contact administrator * 在服務器中, 你可以:define( ENVIRONMENT , production if(! $db- query( $query ) if(ENVIRONMENT == development ) echo $query failed else echo Database error. Please contact administrator }
4,使用可跨平臺的函數執行命令
system, exec, passthru, shell_exec 這4個函數可用于執行系統命令 * Method to execute a command in the terminal * Uses : * 1. system * 2. passthru * 3. exec * 4. shell_execfunction terminal($command)//systemif (function_exists( system )) { ob_start(); // 打開緩沖區 system($command, $return_var); $output = ob_get_contents(); ob_end_clean(); // 清空(擦除)緩沖區并關閉輸出緩沖} //passthruelse if (function_exists( passthru )) { ob_start(); passthru($command, $return_var); $output = ob_get_contents(); ob_end_clean();} //execelse if (function_exists( exec )) { exec($command, $output, $return_var); $output = implode( /n , $output);} //shell_execelse if (function_exists( shell_exec )) { $output = shell_exec($command);} else { $output = Command execution not possible on this system $return_var = 1;return array( output = $output, status = $return_var);terminal( ls
5,靈活編寫函數(判斷是否是數組來編寫邏輯)
function add_to_cart($item_id, $qty) if (!is_array($item_id)) { $_SESSION[ cart ][ item_id ] = $qty; } else { foreach ($item_id as $i_id = $qty) { $_SESSION[ cart ][ i_id ] = $qty;add_to_cart( IPHONE3 , 2);add_to_cart(array( IPHONE3 = 2, IPAD = 5));
6,有意忽略php關閉標簽
like: ?php ......................
7, 在某地方收集所有輸入, 一次輸出給瀏覽器 重點
你可以存儲在函數的局部變量中, 也可以使用ob_start和ob_end_clean
8,發送正確的mime類型頭信息, 如果輸出非html內容的話. 重點
$xml = ?xml version= 1.0 encoding= utf-8 standalone= yes ? $xml = response code 0 /code /response //Send xml dataheader( content-type: text/xml //注意header頭部echo $xml;
9,為mysql連接設置正確的字符編碼
mysqli_set_charset(UTF8);
10,使用 htmlentities 設置正確的編碼選項 重點
php5.4前, 字符的默認編碼是ISO-8859-1, 不能直接輸出如à a等.$value = htmlentities($this- value , ENT_QUOTES , CHARSET);php5.4后, 默認編碼為UTF-8, 這將解決很多問題. 但如果你的應用是多語言的, 仍要留意編碼問題.
11,不要在應用中使用gzip壓縮輸出, 讓apache處理 重點
使用apache的mod_gzip/mod_deflate 模塊壓縮內容. 開啟就行了。用途:壓縮和解壓縮swf文件的代碼等,PHP的zip擴展也行
12,使用json_encode輸出動態javascript內容 而不是 echo
13,寫文件前, 檢查目錄寫權限
linux系統is_readable($file_path)is_writable($file_path)
14,更改應用創建的文件權限
chmod( /somedir/somefile , 0755);
15,不要依賴submit按鈕值來檢查表單提交行為
if( $_SERVER[ REQUEST_METHOD ] == POST and isset($_POST[ submit ]) ) //Save the things}
16,為函數內總具有相同值的變量定義成靜態變量
static $sync_delay = null;
17,不要直接使用 $_SESSION 變量
不同的應用之前加上 不同的 前綴
18,將工具函數封裝到類中(同個類維護多個版本, 而不導致沖突)
class Utility public static function utility_a() public static function utility_b() public static function utility_c() $a = Utility::utility_a(); $b = Utility::utility_b();
19,Bunch of silly tips
使用echo取代print 使用str_replace取代preg_replace, 除非你絕對需要 不要使用 short tag 簡單字符串用單引號取代雙引號 head重定向后記得使用exit 不要在循環中調用函數 isset比strlen快 始中如一的格式化代碼 不要刪除循環或者if-else的括號鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答