首頁 後端開發 php教程 给初学者的30条PHP最佳实践(荒野无灯)_php技巧

给初学者的30条PHP最佳实践(荒野无灯)_php技巧

May 17, 2016 am 09:16 AM
最佳實踐

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. 总是使用
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.

复制代码 代码如下:






untitled







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 include($cachefile);
echo "";
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.使用缓存系统
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等都是好去处。
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PHP中處理字串轉浮點數的最​​佳實踐 PHP中處理字串轉浮點數的最​​佳實踐 Mar 28, 2024 am 08:18 AM

在PHP中處理字串轉浮點數是開發過程中常見的需求,例如從資料庫讀取到的金額欄位是字串類型,需要轉換為浮點數進行數值計算。在這篇文章中,我們將介紹PHP中處理字串轉浮點數的最​​佳實踐,並給出具體的程式碼範例。首先,我們需要明確一點,PHP中的字串轉浮點數有兩種主要的方式:使用(float)型別轉換或使用(floatval)函數。下面我們將分別來介紹這兩

Golang中字符串拼接的最佳實踐是什麼? Golang中字符串拼接的最佳實踐是什麼? Mar 14, 2024 am 08:39 AM

Golang中字符串拼接的最佳实践是什么?在Golang中,字符串拼接是一种常见的操作,但是要考虑到效率和性能。在处理大量字符串拼接时,选择合适的方法可以显著提升程序的性能。下面将介绍几种Golang中字符串拼接的最佳实践,并附上具体的代码示例。使用strings包的Join函数在Golang中,使用strings包的Join函数是一种高效的字符串拼接方法。

探討在Go語言中縮排的最佳實踐 探討在Go語言中縮排的最佳實踐 Mar 21, 2024 pm 06:48 PM

在Go語言中,良好的縮排是程式碼可讀性的關鍵。在編寫程式碼時,統一的縮排風格能夠使程式碼更加清晰、易於理解。本文將探討在Go語言中縮排的最佳實踐,並提供具體的程式碼範例。使用空格而不是製表符在Go語言中,建議使用空格而不是製表符進行縮排。這樣可以避免不同編輯器中製表符寬度不一致所導致的排版問題。縮排的空格數Go語言官方建議使用4個空格作為縮排的空格數。這樣可以使程式碼在

深入比較:Java框架與其他語言框架的最佳實踐 深入比較:Java框架與其他語言框架的最佳實踐 Jun 04, 2024 pm 07:51 PM

Java框架適用於跨平台、穩定性和可擴展性至關重要的專案。對於Java項目,SpringFramework用於依賴注入和麵向方面編程,最佳實踐包括使用SpringBean和SpringBeanFactory。 Hibernate用於物件關係映射,最佳實踐是使用HQL進行複雜查詢。 JakartaEE用於企業應用開發,最佳實踐是使用EJB進行分散式業務邏輯。

golang框架有哪些最佳實踐 golang框架有哪些最佳實踐 Jun 01, 2024 am 10:30 AM

在使用Go框架時,最佳實踐包括:選擇輕量級框架,如Gin或Echo。遵循RESTful原則,使用標準HTTP動詞和格式。利用中間件簡化任務,如身份驗證和日誌記錄。正確處理錯誤,使用錯誤類型和有意義的訊息。編寫單元測試和整合測試,確保應用程式正常運作。

PHP最佳實踐:避免goto語句的替代方案探討 PHP最佳實踐:避免goto語句的替代方案探討 Mar 28, 2024 pm 04:57 PM

PHP最佳實踐:避免goto語句的替代方案探討在PHP程式設計中,goto語句是一種控制結構,它允許直接跳到程式中的另一個位置。雖然goto語句可以簡化程式碼結構和流程控制,但由於其使用容易導致程式碼混亂、可讀性降低以及除錯困難等問題,因此被廣泛認為是一種不良實踐。在實際開發中,為避免使用goto語句,我們需要尋找替代方法來實現相同的功能。本文將探討一些替代方案,

Laravel開發中.env檔的作用及最佳實踐 Laravel開發中.env檔的作用及最佳實踐 Mar 10, 2024 pm 03:03 PM

Laravel開發中.env文件的作用及最佳實踐在Laravel應用程式開發中,.env文件被認為是非常重要的文件之一。它承載著一些關鍵的配置訊息,例如資料庫連接資訊、應用程式環境、應用程式金鑰等。在本文中,我們將深入探討.env檔案的作用以及最佳實踐,並附上具體的程式碼範例。 1..env檔的作用首先,我們需要了解.env檔的作用。在一個Laravel應

Git 還是版本控制? PHP 專案管理中的關鍵區別 Git 還是版本控制? PHP 專案管理中的關鍵區別 Mar 10, 2024 pm 01:04 PM

版本控制:基礎版本控制是一種軟體開發實踐,允許團隊追蹤程式碼庫中的變更。它提供了一個中央儲存庫,其中包含專案文件的所有歷史版本。這使開發人員能夠輕鬆回滾錯誤,查看不同版本的差異,並協調對程式碼庫的並發變更。 Git:分散式版本控制系統git是一種分散式版本控制系統(DVCS),這表示每個開發人員的電腦都擁有整個程式碼庫的完整副本。這消除了對中心伺服器的依賴,提高了團隊的靈活性和協作能力。 Git允許開發人員建立和管理分支,追蹤程式碼庫的歷史,並與其他開發者共用變更。 Git與版本控制:關鍵區別分散式vs集

See all articles