Home | 简体中文 | 繁体中文 | 杂文 | Search | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 作品与服务 | Email

Chapter 17. 编码风格

Table of Contents

17.1. php 文件
17.1.1. 格式与 编码
17.1.2. 循环嵌套
17.1.3. 取出行尾的空格以及多余的换行符
17.1.4. php 标签
17.1.5. 头部注释
17.2. String
17.3. Database
17.3.1. 结果集使用注意事项
17.3.2. 索引
17.3.3. 缓存
https://code.google.com/p/google-styleguide/
http://lxr.linux.no/linux/Documentation/CodingStyle
http://perldoc.perl.org/perlstyle.html
http://www.gnu.org/prep/standards/
	

17.1. php 文件

17.1.1. 格式与 编码

使用 UNIX 风格换行, 请在你的编辑器内调整

UNIX (LF或"\n")
MAC OS (CR 或"\r")
Windows CRLF \r\n
			

源码文件使用 UTF-8

有些IDE环境 UTF-8 BOM

17.1.2. 循环嵌套

if, while, for, foreach, do ... loop, switch... 等的嵌套必须小于等于3层

如下面的例子,可读性极差。

if (xxx){
	if (xxx){
		if(xxx){
			if(xxx){
				if(xxx){

				}
			}
		}
		if(xxx){
			if(xxx){
			}
		}
	}
	if (xxx){
		if(xxx){
			if(xxx){
			}
		}
		if(xxx){
			if(xxx){
			}
		}
	}
}
			

加以改造

func aaa(p){
	if(p){
		if(xxx){
		}
	}
}
func bbb(b){
	if(b){
		if(xxx){
			if(xxx){

			}
		}
	}
}

if(xxx){
	aaa(xxx)
}
if(b){
	bbb(b)
}			

17.1.3. 取出行尾的空格以及多余的换行符

一个空格占用一个字节,换行符Window是两个字节\r\n, Unix与Mac占用一个字节

17.1.4. php 标签

禁止这样使用

			
<?
...
?>
			
			

正确的使用方法

			
<?php
...

or

<?php
...
?>
			
			

17.1.5. 头部注释

			
<?php
/**
 * Project Name
 *
 * @author     $Author: netkiller $
 * @copyright  Copyright (c) 2012 Company
 * @version    $Id: chapter.coding.xml 584 2013-05-15 05:13:17Z netkiller $
 */

<?php
/**
 * Project Name
 *
 * @author     $Author: netkiller $
 * @license    GNU General Public License 2.0
 * @version    $Id: chapter.coding.xml 584 2013-05-15 05:13:17Z netkiller $
 */
			
			
comments powered by Disqus