欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-11 16:09:27
1,和php手册成为好朋友 2,打开error reporting error reporting 在 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.使用缓存系统
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.勇敢地提问
*等都是好去处。