初学者的30条PHP最佳实践
学会php可以在很多地方使用,比如做一个后台登录的功能,要让程序记住用户,其实很简单,看了下面的文章你就明白了。给初学者的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)
-
php递归创建和删除文件夹的代码
php中递归创建和删除文件夹的代码,供大家学习参考。就跟随本站小编一起去了解下吧,想了解更多相关信息请持续关注我们应届毕业生考试网!方法一复制代码代码如下:<?php/***目录生成类:UtilsMakeDir*@authoryepeng*@since2010.3.18*/classUtilsMakeDir{//基目录建...
-
PHP的SPL标准库的用法介绍
本文介绍下,php编程中SPL中的用法,SPL,PHP标准库(StandardPHPLibrary),此从PHP5.0起内置的组件和接口,有需要的朋友参考下。PHPSPL的用法SPL,PHP标准库(StandardPHPLibrary),此从PHP5.0起内置的组件和接口,并且从PHP5.3已逐渐的成熟。SPL其实在所有的PHP5开发环境中被内置,...
-
PHP中iconv函数知识
iconv函数库能够完成各种字符集间的转换,是php编程中不可缺少的基础函数库。下面是小编为大家搜集整理出来的有关于PHP中iconv函数知识,有需要的小伙伴可以参考下。今天在修改论文在线的时候,遇到了iconv这个函数。学习一下1234header('Content-Type:applicat...
-
PHP字符串函数大汇总
我们大家知道无论哪种编程语言,字符串操作都是一个重要的基础,往往是简单而重要。PHP给我们提供了大量的字符串操作函数,功能强大,使用也比较简单,在这里小编为大家总结九类字符串函数。1.查找字符位置函数strpos($str,search,[int]):查找search在$str中的第一次位...