天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

給初學者的30條PHP最佳實踐(荒野無燈)

1,和php手冊成為好朋友
2,打開Error Reporting
Error reporting 在 php 開發時是很有幫助的. 你可以在你代碼中發現先前你沒有發現的錯誤,因為并不是所有的BUG都會讓程序運行不了的。當產品正式使用時,才有必要關掉錯誤報告,不然顧客看到一堆奇怪的字符不知道那是什么意思。
3,使用IDE
IDE (集成開發環境,Integrated Development Environments)對于開發者來說是很有幫助的工具.
荒野在這里推薦NETbeans IDE 。
4. 試著使用一個php 框架
5.學習DRY方法
DRY 代表 Don't Repeat Yourself,它是一個有價值的編程概念,不管是什么語言。DRY編程,顧名思義,是確保你不寫多余的代碼。
6.使用空格縮進代碼來提高可讀性
7. “Tier” your Code
給你的應用程序分層,分成不同部位的不同組成部分的代碼。這使得您可以輕松地在未來改變你的代碼。 如常用的MVC模式。
8. 總是使用 <?php ?>
9.使用有意義的,一致的命名約定
10.注釋、注釋、注釋
11.安裝MAMP/WAMP
12.給你的腳本限制運行時間
通常php腳本的運行時間被限制為30秒,超過這個時間php將拋出一個致命錯誤。
13.使用OOP
14.知道雙引號和單引號的不同
15.不要在網站的根目錄放phpinfo()
16.永遠不要信任你的用戶
17.加密存儲密碼
Rebuttal:
Keep in mind, however, that MD5 hashes have long since been compromised. They're absolutely more secure than not, but, with the use of an enormous “rainbow table,” hackers can cross reference your hash. To add even more security, consider adding a salt as well. A salt is basically an additional set of characters that you append to the user's string.
18.使用可視化數據庫設計工具
如 DBDesigner 和 MySQL Workbench
19.使用輸出緩沖
Rebuttal: Though not required, it's generally considered to be a good practice to go ahead and append the “ob_end_flush();” function as well to the bottom of the document. P.S. Want to compress the HTML as well? Simply replace “ob_start();” with “ob_start(‘ob_gzhandler')”;
Refer to this Dev-tips article for more information.
復制代碼 代碼如下:
<!DOCTYPE html>
<?php ob_start('ob_gzhandler'); ?>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
</head>
<body>

</body>
</html>
<?php ob_end_flush(); ?>

20.保護你的代碼避免SQL注射
復制代碼 代碼如下:
$username = mysql_real_escape_string( $GET['username'] );
  $id = $_GET['id'];
$statement = $connection->prepare( "SELECT * FROM tbl_members WHERE id = ?" );
$statement->bind_param( "i", $id );
$statement->execute();

By using prepared statements, we never embed the user's inputted data directly into our query. Instead, we use the “bind_param” method to bind the values (and escaping) to the query. Much safer, and, notably, faster when executing multiple CRUD statements at once.
21.嘗試ORM (object relational mapping)
ORM libraries for php like Propel, and ORM is built into php frameworks like Cakephp.
22.緩存數據庫驅動頁面
如:
復制代碼 代碼如下:
// TOP of your script
$cachefile = 'cache/'.basename($_SERVER['SCRIPT_URI']);
$cachetime = 120 * 60; // 2 hours
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
include($cachefile);
echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->";
exit;
}
ob_start(); // start the output buffer
// Your normal php script and HTML content here
// BOTTOM of your script
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser

23.使用緩存系統
  • Memcached
  • APC
  • XCache
  • Zend Cache
  • eAccelerator
24.驗證Cookie數據
Cookie data, like any data passed on the Web, can be harmful. You can validate cookie data with either the htmlspecialchars() or mysql_real_escape_string().
25.使用靜態文件緩存系統
如Smarty的是一個內置緩存的強大的模板系統。
26.分析你的代碼
Profiling your code with a tool like xdebug can help you to quickly spot bottlenecks and other potential problems in your php code. Some IDEs like NETbeans have php profiling capabilities as well.
27.編碼標準
如 Pear標準。
28. Keep Functions Outside of Loops
You take a hit of performance when you include functions inside of loops. The larger the loop that you have, the longer the execution time will take. Take the extra time and line of code and place the function outside of the loop.
Editor's Note: Think of it this way. Try to remove as many operations from the loop as possible. Do you really need to create that variable for every iteration of the loop? Do you really need to create the function each time? Of course not.
29.不要復制不額外的變量(事實上這一條值得懷疑,見下面的說明)
如:
復制代碼 代碼如下:
$description = strip_tags($_POST['description']);
echo $description;

可以寫成如下:
echo strip_tags($_POST['description']);
Rebuttal: In reference to the comment about “doubling the memory,” this actually is a common misconception. php implements “copy-on-write” memory management. This basically means that you can assign a value to as many variables as you like without having to worry about the data actually being copied. While it's arguable that the “Good” example exemplified above might make for cleaner code, I highly doubt that it's any quicker.
也就是說php實現“copy-on-write” 的內存管理方式,上面第一種代碼并不會存在占用雙倍內存的情況。因此Rebuttal嚴重懷疑第二種方式的代碼是否真的比前面的快。
30.更新到最新版本的php
31.減少數據庫查詢次數
32.勇敢地提問
像StackOverflow等都是好去處。

php技術給初學者的30條PHP最佳實踐(荒野無燈),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 老女老肥熟国产在线视频 | 二级片免费看 | 黑人操白逼 | 久久国产香蕉 | 一边喂奶一边做边爱 | 国产睡熟迷奷系列网站 | 亚洲不卡高清免v无码屋 | 老师小扫货水能么多叫出来 | 免费精品一区二区三区在线观看 | 日本国产精品无码一区免费看 | 久久久久久亚洲精品影院 | 亚洲国产成人精品无码区APP | 久久人妻少妇嫩草AV蜜桃99 | 亚洲精品在线不卡 | 毛片无码免费无码播放 | 国产色婷婷精品人妻蜜桃成熟 | 久久超碰国产精品最新 | 国产亚洲精品97在线视频一 | 精品久久久久久无码人妻国产馆 | av在线观看网站免费 | 一天不停的插BB十几次 | 久久精品国产午夜伦班片 | 久久综合久久伊人 | 二色AV天堂在线 | 精品熟女少妇AV久久免费A片 | 香蕉 在线播放 | 三级黄色在线视频中文 | 国模玲玲自拍337p | 男女无遮挡吃奶gift动态图 | 国产在线一区观看 | 国产AV亚洲精品久久久久 | free性中国hd护士高清 | 成人影院午夜久久影院 | 国产日韩久久久精品影院首页 | 亚洲欧美成人 | 狠狠色噜噜狠狠狠狠米奇777 | my pico未删减在线观看 | 国产乱人视频在线观看 | 亚洲 自拍 偷拍 另类综合图区 | 国产亚洲欧洲日韩在线三区 | 任你躁国语自产二区在线播放 |