1.不要使用相对路径

常常会看到:

1
require_once('../../lib/some_class.php');

该方法有很多缺点: 它首先查找指定的php包含路径, 然后查找当前目录。因此会检查过多路径。如果该脚本被另一目录的脚本包含, 它的基本目录变成了另一脚本所在的目录.

另一问题, 当定时任务运行该脚本, 它的上级目录可能就不是工作目录了。因此最佳选择是使用绝对路径:

1
2
3
4
view sourceprint?
 define('ROOT' , '/var/www/project/');
 require_once(ROOT . '../../lib/some_class.php');
 //rest of the code

我们定义了一个绝对路径, 值被写死了. 我们还可以改进它. 路径 /var/www/project 也可能会改变, 那么我们每次都要改变它吗? 不是的, 我们可以使用__FILE__常量, 如:

1
2
3
4
5
//suppose your script is /var/www/project/index.php
 //Then __FILE__ will always have that full path.
 define('ROOT' , pathinfo(__FILE__, PATHINFO_DIRNAME));
 require_once(ROOT . '../../lib/some_class.php');
 //rest of the code

现在, 无论你移到哪个目录, 如移到一个外网的服务器上, 代码无须更改便可正确运行.

2. 不要直接使用 require, include, include_once, required_once

可以在脚本头部引入多个文件, 像类库, 工具文件和助手函数等, 如:

1
2
3
require_once('lib/Database.php');
 require_once('lib/Mail.php');
 require_once('helpers/utitlity_functions.php');

这种用法相当原始. 应该更灵活点. 应编写个助手函数包含文件. 例如:

1
2
3
4
5
6
7
8
function load_class($class_name)
 {
 //path to the class file
 $path = ROOT . '/lib/' . $class_name . '.php');
 require_once( $path );
 }
 load_class('Database');
 load_class('Mail');

有什么不一样吗? 该代码更具可读性。將来你可以按需扩展该函数, 如:

1
2
3
4
5
6
7
8
9
function load_class($class_name)
 {
 //path to the class file
 $path = ROOT . '/lib/' . $class_name . '.php');
 if(file_exists($path))
 {
 require_once( $path );
 }
 }

还可做得更多: 为同样文件查找多个目录。能很容易的改变放置类文件的目录, 无须在代码各处一一修改。可使用类似的函数加载文件, 如html内容.

3. 为应用保留调试代码

在开发环境中, 我们打印数据库查询语句, 转存有问题的变量值, 而一旦问题解决, 我们注释或删除它们. 然而更好的做法是保留调试代码。在开发环境中, 你可以:

1
2
3
4
5
6
7
8
9
10
11
12
define('ENVIRONMENT' , 'development');
 if(! $db->query( $query )
 {
 if(ENVIRONMENT == 'development')
 {
 echo "$query failed";
 }
 else
 {
 echo "Database error. Please contact administrator";
 }
 }

在服务器中, 你可以:

1
2
3
4
5
6
7
8
9
10
11
12
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个函数可用于执行系统命令. 每个的行为都有细微差别. 问题在于,
当在共享主机中, 某些函数可能被选择性的禁用. 大多数新手趋于每次首先检查哪个函数可用, 然而再使用它。更好的方案是封成函数一个可跨平台的函数.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 Method to execute a command in the terminal
 Uses :
 1. system
 2. passthru
 3. exec
 4. shell_exec
 */
 function terminal($command)
 {
 //system
 if(function_exists('system'))
 {
 ob_start();
 system($command , $return_var);
 $output = ob_get_contents();
 ob_end_clean();
 }
 //passthru
 else if(function_exists('passthru'))
 {
 ob_start();
 passthru($command , $return_var);
 $output = ob_get_contents();
 ob_end_clean();
 }
 //exec
 else if(function_exists('exec'))
 {
 exec($command , $output , $return_var);
 $output = implode("\n" , $output);
 }
 //shell_exec
 else 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');

上面的函数將运行shell命令, 只要有一个系统函数可用, 这保持了代码的一致性.

5. 灵活编写函数

1
2
3
4
5
6
function add_to_cart($item_id , $qty)
   
{
 $_SESSION['cart']['item_id'] = $qty;
 }
add_to_cart( 'IPHONE3' , 2 );

使用上面的函数添加单个项目. 而当添加项列表的时候,你要创建另一个函数吗? 不用, 只要稍加留意不同类型的参数, 就会更灵活. 如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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关闭标签

我很想知道为什么这么多关于php建议的博客文章都没提到这点.

1
2
3
<?php
 echo "Hello";
 //Now dont close this tag

这將节约你很多时间. 我们举个例子:

一个 super_class.php 文件

1
2
3
4
5
6
7
8
9
<?php
 class super_class
 {
 function super_function()
 {
 //super code
 }
 }
 ?>

//super extra character after the closing tag

index.php

1
2
require_once('super_class.php');
 //echo an image or pdf , or set the cookies or session data

这样, 你將会得到一个 Headers already send error. 为什么? 因为 “super extra
character” 已经被输出了. 现在你得开始调试啦. 这会花费大量时间寻找 super extra 的位置。因此, 养成省略关闭符的习惯:

1
2
3
4
5
6
7
8
9
<?php
 class super_class
 {
 function super_function()
 {
 //super code
 }
 }
 //No closing tag

这会更好.

7. 在某地方收集所有输入, 一次输出给浏览器

这称为输出缓冲, 假如说你已在不同的函数输出内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function print_header()
 {
 echo "<div id='header'>Site Log and Login links</div>";
 }
 function print_footer()
 {
 echo "<div id='footer'>Site was made by me</div>";
 }
 print_header();
 for($i = 0 ; $i < 100; $i++)
 {
 echo "I is : $i ';
 }
print_footer();

替代方案, 在某地方集中收集输出. 你可以存储在函数的局部变量中, 也可以使用ob_start和ob_end_clean. 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function print_header()
 {
 $o = "<div id='header'>Site Log and Login links</div>";
 return $o;
 }
function print_footer()
 {
 $o = "<div id='footer'>Site was made by me</div>";
 return $o;
 }
echo print_header();
 for($i = 0 ; $i < 100; $i++)
 {
 echo "I is : $i ';
 }
 echo print_footer();

为什么需要输出缓冲:

>>可以在发送给浏览器前更改输出. 如 str_replaces 函数或可能是 preg_replaces 或添加些监控/调试的html内容.

>>输出给浏览器的同时又做php的处理很糟糕. 你应该看到过有些站点的侧边栏或中间出现错误信息. 知道为什么会发生吗? 因为处理和输出混合了.

8. 发送正确的mime类型头信息, 如果输出非html内容的话.

输出一些xml.

1
2
3
4
5
6
$xml = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
 $xml = "<response>
 <code>0</code>
 </response>";
//Send xml data
 echo $xml;

工作得不错. 但需要一些改进.

1
2
3
4
5
6
7
$xml = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
 $xml = "<response>
 <code>0</code>
 </response>";
 //Send xml data
 header("content-type: text/xml");
 echo $xml;

注意header行. 该行告知浏览器发送的是xml类型的内容. 所以浏览器能正确的处理. 很多的javascript库也依赖头信息.

类似的有 javascript , css, jpg image, png image:

1
2
3
4
5
6
JavaScript
header("content-type: application/x-javascript");
 echo "var a = 10";
 CSS
header("content-type: text/css");
 echo "#div id { background:#000; }";

9. 为mysql连接设置正确的字符编码

曾经遇到过在mysql表中设置了unicode/utf-8编码, phpadmin也能正确显示, 但当你获取内容并在页面输出的时候,会出现乱码. 这里的问题出在mysql连接的字符编码.

1
2
3
4
5
6
7
8
9
10
11
12
//Attempt to connect to database
 $c = mysqli_connect($this->host , $this->username, $this->password);
 //Check connection validity
 if (!$c)
 {
 die ("Could not connect to the database host: ". mysqli_connect_error());
 }
 //Set the character set of the connection
if(!mysqli_set_charset ( $c , 'UTF8' ))
 {
 die('mysqli_set_charset() failed');
 }

一旦连接数据库, 最好设置连接的 characterset. 你的应用如果要支持多语言, 这么做是必须的.

10. 使用 htmlentities 设置正确的编码选项

php5.4前, 字符的默认编码是ISO-8859-1, 不能直接输出如À â等.

1
$value = htmlentities($this->value , ENT_QUOTES , CHARSET);

php5.4以后, 默认编码为UTF-8, 这將解决很多问题. 但如果你的应用是多语言的, 仍然要留意编码问题,.

11. 不要在应用中使用gzip压缩输出, 让apache处理

考虑过使用 ob_gzhandler 吗? 不要那样做. 毫无意义. php只应用来编写应用. 不应操心服务器和浏览器的数据传输优化问题.

使用apache的mod_gzip/mod_deflate 模块压缩内容.

12. 使用json_encode输出动态javascript内容

时常会用php输出动态javascript内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$images = array(
 'myself.png' , 'friends.png' , 'colleagues.png'
 );
$js_code = '';
foreach($images as $image)
{
$js_code .= "'$image' ,";
}
$js_code = 'var images = [' . $js_code . ']; ';
echo $js_code;
//Output is var images = ['myself.png' ,'friends.png' ,'colleagues.png' ,];
   
更聪明的做法, 使用 json_encode:
   
$images = array(
 'myself.png' , 'friends.png' , 'colleagues.png'
 );
$js_code = 'var images = ' . json_encode($images);
 echo $js_code;
//Output is : var images = ["myself.png","friends.png","colleagues.png"]

优雅乎?

13. 写文件前, 检查目录写权限

写或保存文件前, 确保目录是可写的, 假如不可写, 输出错误信息. 这会节约你很多调试时间. linux系统中, 需要处理权限, 目录权限不当会导致很多很多的问题, 文件也有可能无法读取等等.

确保你的应用足够智能, 输出某些重要信息.

1
2
3
$contents = "All the content";
$file_path = "/var/www/project/content.txt";
 file_put_contents($file_path , $contents);

这大体上正确. 但有些间接的问题. file_put_contents 可能会由于几个原因失败:

>>父目录不存在

>>目录存在, 但不可写

>>文件被写锁住?

所以写文件前做明确的检查更好.

1
2
3
4
5
6
7
8
9
10
11
$contents = "All the content";
 $dir = '/var/www/project';
 $file_path = $dir . "/content.txt";
 if(is_writable($dir))
 {
 file_put_contents($file_path , $contents);
 }
 else
 {
 die("Directory $dir is not writable, or does not exist. Please check");
 }

这么做后, 你会得到一个文件在何处写及为什么失败的明确信息.

14. 更改应用创建的文件权限

在linux环境中, 权限问题可能会浪费你很多时间. 从今往后, 无论何时, 当你创建一些文件后,
确保使用chmod设置正确权限. 否则的话, 可能文件先是由”php”用户创建, 但你用其它的用户登录工作, 系统將会拒绝访问或打开文件,
你不得不奋力获取root权限, 更改文件的权限等等.

1
2
3
4
// Read and write for owner, read for everybody else
 chmod("/somedir/somefile", 0644);
 // Everything for owner, read and execute for others
 chmod("/somedir/somefile", 0755);

15. 不要依赖submit按钮值来检查表单提交行为

1
2
3
4
if($_POST['submit'] == 'Save')
 {
 //Save the things
 }

上面大多数情况正确, 除了应用是多语言的. ‘Save' 可能代表其它含义. 你怎么区分它们呢. 因此, 不要依赖于submit按钮的值.

1
2
3
4
if( $_SERVER['REQUEST_METHOD'] == 'POST' and isset($_POST['submit']) )
 {
 //Save the things
 }

现在你从submit按钮值中解脱出来了.

16. 为函数内总具有相同值的变量定义成静态变量

1
2
3
4
5
6
7
8
//Delay for some time
 function delay()
 {
 $sync_delay = get_option('sync_delay');
 echo "Delaying for $sync_delay seconds...";
 sleep($sync_delay);
 echo "Done ";
 }

用静态变量取代:

1
2
3
4
5
6
7
8
9
10
11
12
//Delay for some time
 function delay()
 {
 static $sync_delay = null;
 if($sync_delay == null)
 {
 $sync_delay = get_option('sync_delay');
 }
 echo "Delaying for $sync_delay seconds...";
 sleep($sync_delay);
 echo "Done ";
 }

17. 不要直接使用 $_SESSION 变量

某些简单例子:

1
2
$_SESSION['username'] = $username;
 $username = $_SESSION['username'];

这会导致某些问题. 如果在同个域名中运行了多个应用, session 变量可能会冲突. 两个不同的应用可能使用同一个session key. 例如, 一个前端门户, 和一个后台管理系统使用同一域名。从现在开始, 使用应用相关的key和一个包装函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
define('APP_ID' , 'abc_corp_ecommerce');
 //Function to get a session variable
 function session_get($key)
 {
 $k = APP_ID . '.' . $key;
 if(isset($_SESSION[$k]))
 {
 return $_SESSION[$k];
 }
 return false;
 }
 //Function set the session variable
 function session_set($key , $value)
 {
 $k = APP_ID . '.' . $key;
 $_SESSION[$k] = $value;
 return true;
 }

18. 將工具函数封装到类中

假如你在某文件中定义了很多工具函数:

1
2
3
4
5
6
7
8
9
10
11
12
function utility_a()
 {
 //This function does a utility thing like string processing
 }
function utility_b()
 {
 //This function does nother utility thing like database processing
 }
 function utility_c()
 {
 //This function is ...
 }

这些函数的使用分散到应用各处. 你可能想將他们封装到某个类中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Utility
 {
public static function utility_a()
{
}
public static function utility_b()
 {
 }
public static function utility_c()
 {
 }
 }
//and call them as
 $a = Utility::utility_a();
 $b = Utility::utility_b();

显而易见的好处是, 如果php内建有同名的函数, 这样可以避免冲突.

另一种看法是, 你可以在同个应用中为同个类维护多个版本, 而不导致冲突. 这是封装的基本好处, 无它.

19. Bunch of silly tips

>>使用echo取代print

>>使用str_replace取代preg_replace, 除非你绝对需要

>>不要使用 short tag

>>简单字符串用单引号取代双引号

>>head重定向后记得使用exit

>>不要在循环中调用函数

>>isset比strlen快

>>始中如一的格式化代码

>>不要删除循环或者if-else的括号

不要这样写代码:

1
<span style="color:#333333;font-family:''Helvetica, Arial, sans-serif'';">if($a == true) $a_count++;</span>

这绝对WASTE。写成:

1
2
3
4
<span style="color:#333333;font-family:''Helvetica, Arial, sans-serif'';">if($a == true)
 {
 $a_count++;
 }</span>

不要尝试省略一些语法来缩短代码. 而是让你的逻辑简短.

>>使用有高亮语法显示的文本编辑器. 高亮语法能让你减少错误.

20. 使用array_map快速处理数组

比如说你想 trim 数组中的所有元素. 新手可能会:

1
2
3
4
foreach($arr as $c => $v)
 {
 $arr[$c] = trim($v);
 }

但使用 array_map 更简单:

1
$arr = array_map('trim' , $arr);

这会为$arr数组的每个元素都申请调用trim. 另一个类似的函数是 array_walk. 请查阅文档学习更多技巧.

21. 使用 php filter 验证数据

你肯定曾使用过正则表达式验证 email , ip地址等. 是的,每个人都这么使用. 现在, 我们想做不同的尝试, 称为filter.

php的filter扩展提供了简单的方式验证和检查输入.

22. 强制类型检查

1
2
$amount = intval( $_GET['amount'] );
$rate = (int) $_GET['rate'];

这是个好习惯.

23. 如果需要,使用profiler如xdebug

如果你使用php开发大型的应用, php承担了很多运算量, 速度会是一个很重要的指标. 使用profile帮助优化代码. 可使用xdebug和webgrid.

24. 小心处理大数组

对于大的数组和字符串, 必须小心处理. 常见错误是发生数组拷贝导致内存溢出,抛出Fatal Error of Memory size 信息:

1
2
3
$db_records_in_array_format; //This is a big array holding 1000 rows from a table each having 20 columns , every row is atleast 100 bytes , so total 1000 * 20 * 100 = 2MB
$cc = $db_records_in_array_format; //2MB more
some_function($cc); //Another 2MB ?

当导入或导出csv文件时, 常常会这么做。不要认为上面的代码会经常因内存限制导致脚本崩溃. 对于小的变量是没问题的, 但处理大数组的时候就必须避免.

确保通过引用传递, 或存储在类变量中:

1
2
$a = get_large_array();
 pass_to_function(&$a);

这么做后, 向函数传递变量引用(而不是拷贝数组). 查看文档.

1
2
3
4
5
6
7
8
9
10
11
12
class A
 {
 function first()
 {
 $this->a = get_large_array();
 $this->pass_to_function();
 }
 function pass_to_function()
 {
 //process $this->a
 }
 }

尽快的 unset 它们, 让内存得以释放,减轻脚本负担.

25. 由始至终使用单一数据库连接

确保你的脚本由始至终都使用单一的数据库连接. 在开始处正确的打开连接, 使用它直到结束, 最后关闭它. 不要像下面这样在函数中打开连接:

1
2
3
4
5
6
7
8
9
10
function add_to_cart()
 {
$db = new Database();
 $db->query("INSERT INTO cart .....");
 }
 function empty_cart()
 {
$db = new Database();
 $db->query("DELETE FROM cart .....");
 }

使用多个连接是个糟糕的, 它们会拖慢应用, 因为创建连接需要时间和占用内存。特定情况使用单例模式, 如数据库连接.。

这个秘籍是不是很精彩,相信一定对大家学习php程序设计有所帮助。

高质量PHP代码的50个实用技巧必备(上)的更多相关文章

  1. 高质量PHP代码的50个实用技巧必备(下)

    26. 避免直接写SQL, 抽象之 不厌其烦的写了太多如下的语句: ? 1 2 <span style="color:#333333;font-family:''Helvetica, ...

  2. 高质量PHP代码的50个实用技巧:非常值得收藏

    http://www.finalshares.com/read-497?qq-2488

  3. 深入理解JavaScript系列(1):编写高质量JavaScript代码的基本要点

    深入理解JavaScript系列(1):编写高质量JavaScript代码的基本要点 2011-12-28 23:00 by 汤姆大叔, 139489 阅读, 119 评论, 收藏, 编辑 才华横溢的 ...

  4. 编写高质量JS代码的68个有效方法(八)

    [20141227]编写高质量JS代码的68个有效方法(八) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  5. 高质量JavaScript代码书写基本要点

    翻译-高质量JavaScript代码书写基本要点 by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/ ...

  6. 编写高质量JavaScript代码的68个有效方法

    简介: <Effective JavaScript:编写高质量JavaScript代码的68个有效方法>共分为7章,分别涵盖JavaScript的不同主题.第1章主要讲述最基本的主题,如版 ...

  7. Effective Python之编写高质量Python代码的59个有效方法

                                                         这个周末断断续续的阅读完了<Effective Python之编写高质量Python代码 ...

  8. 编写高质量JS代码的68个有效方法(七)

    [20141220]编写高质量JS代码的68个有效方法(七) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

  9. 编写高质量JS代码的68个有效方法(六)

    [20141213]编写高质量JS代码的68个有效方法(六) *:first-child { margin-top: 0 !important; } body>*:last-child { ma ...

随机推荐

  1. spacemacs 初始安装报错

    尝试使用emcas的配置文件spacemacs,第一次安装启动时,界面为纯白色,而且在输入完几个配置选项后,报了一个错误 Symbol's value as variable is void 根据官网 ...

  2. 本地文件与服务器文件同步shell脚本。

    #!/bin/sh read -t 30 -p "请输入项目名:" name echo -e "\n" echo "项目名为:$name" ...

  3. Oil Deposits

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

  4. Best Coder #86 1002 NanoApe Loves Sequence

    NanoApe Loves Sequence Accepts: 531 Submissions: 2481 Time Limit: 2000/1000 MS (Java/Others) Memory ...

  5. 一个基于EntityFramework Core的简单数据库访问层,适用于轻量级数据库业务

    这个访问层的代码实际上是园子里某个前辈的,本人只是觉得好使,记录了下来. 本访问层需要通过Nuget安装EntityFramework Core,不过个人认为EF 6同样可以使用. 搭配数据库,最好是 ...

  6. 行内脚本的位置放置----css阻塞行内脚本

    行内脚本:避免放置在css和其他资源之间. 若在head中,最好放在css样式表之前,如果放置在样式表之后,会引起css阻塞. css阻塞:由于浏览器要保证css和JavaScript的执行顺序,cs ...

  7. javascript 关于 this 作用域链

    使用 function f() {}  或者 var f = function() {}  来定义的函数,this 是指向 全局对象   var  a = {    b: 1,    c: funct ...

  8. ⑦bootstrap按钮 图片 辅助使用基础案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. WebP 的前世今生

    除了视频,图片占据了 PC 和 App 的大部分流量,为运营方带来高额的成本支出,同时过多的图片加载会影响到网站与 App 的加载速度.因此在保证图片质量的前提下缩小图片的体积就成了迫在眉睫的事情. ...

  10. 验证Oracle处理速度

    (这是2009年写的东西了,在网上看到有人对数据库批量操作的'速度'比较关注,于是就把这篇老文章整理了一下) 一.环境及前提 在244上(一台稍好一些的机器,做了RAID,机械硬盘,Raid几忘了), ...