PHP 开发中有哪些不好的编程习惯需要避免
因为
@公子
公子指出了我在一个
回复
中的错误,我才开始逐渐意识到自己已经开始养成了一些很不好的编程习惯,比如嵌套循环,比如
N
多个条件嵌套,导致
}
符号堆积,以至于代码可读性极差。之前只重视了功能的实现与否,没有忽视了代码的可读性和性能方面的体现。
这边罗列下我所知道的一些不好的编程习惯:
1.嵌套循环
2.过多的条件嵌套
3.用户提交数据不进行过滤
4.函数体过长
5.变量命名清晰
我相信有不少
PHPer
都读过
《PHP 最佳实践》
,或许我们大家可以集思广益,总结一个
《PHP“最差”实践》
,这样也可以当做一个警示录,时刻提醒自己优化代码,希望
PHP
开发经验丰富的前辈们能不吝赐教。:)
纯洁诚实小郎君
11 years, 2 months ago
Answers
1,拼凑SQL,如下
$sql = "INSERT INTO `#@__archives`(id,typeid,typeid2,sortrank,flag,ismake,channel,arcrank,click,money,title,shorttitle,
color,writer,source,litpic,pubdate,senddate,mid,voteid,notpost,description,keywords,filename,dutyadmin,weight)
VALUES ('{$this->art['id']}','{$this->art['typeid']}','{$this->art['typeid2']}','{$this->art['sortrank']}','{$this->art['flag']}',
'{$this->art['ismake']}','{$this->art['channelid']}','{$this->art['arcrank']}','{$this->art['click']}','{$this->art['money']}','{$this->art['title']}',
'{$this->art['shorttitle']}','{$this->art['color']}','{$this->art['writer']}','{$this->art['source']}','{$this->art['litpic']}','{$this->art['pubdate']}',
'{$this->art['senddate']}','{$this->art['adminid']}','{$this->art['voteid']}','{$this->art['notpost']}','{$this->art['description']}',
'{$this->art['keywords']}','{$this->art['filename']}','{$this->art['adminid']}','{$this->art['weight']}');";
2, 迟迟不肯return,代码挤在一起
function readHosts(){
global $config;
$hosts = array();
$fp = fopen(HOST_FILE, 'r');
if($fp){
while(!feof($fp)){
$line = trim(fgets($fp));
if($line!='' && substr($line, 0, 1)!='#'){
//将多个空格替换为一个,增加容错率
$line = preg_replace("/\s(?=\s)/","\\1",$line);
@list($host,$user,$password,$time) = explode(' ', $line);
$timeline = mktime((int)$time);
if($host && $user && $password){
if($timeline <= time()){
$hosts[] = array(
'dbhost' => $host,
'dbuser' => $user,
'dbpwd' => $password,
'dbname' => 'mysql',
'dbprefix' => 'dede_',
'dbcharset' => 'gbk',
);
msg('服务器'.$host.'加入发布列队');
}else{
msg('服务器'.$host.'没有到发布的时间');
}
}
}
}
echo "\r\n";
}
fclose($fp);
return $hosts;
}
3, 全局变量global, 以下的
msg
函数大量调用....
function SendToSite($site){
global $db,$log,$config;
if($log->get_value($site['dbname'])==='finish') return;
echo "=====================\r\n";
msg('开始连接到数据库 '.$site['dbname']);
if($db->select_db($site['dbname'])){
msg('连接成功,收集站点信息');
if($db->HasTable('archives')){
msg('找到文章数据表,开始发布');
}else{
msg('没有找到文章数据表,可能表前缀不正确,跳过这个站点');
return;
}
}else{
msg('连接失败,跳过这个站点');
return;
}
4,超长嵌套
if(count($site_channel)>0) {
$keylink = '<a href="'.$site_url.'" target="_blank">'.GBKTOUTF8($site_name).'</a>';
msg('找到'.count($site_channel).'个文章栏目');
echo "\r\n";
foreach ($site_channel as $channel){
if($log->get_value($site['dbname'].'_'.$channel['id'])==='finish'){
msg($site['dbname'].'栏目ID为'.$channel['id'].'今日已发布完成,跳过');
continue;
}
$sendnum = ReadIntFromStr($config['send_num']);
if($sendnum <= $log->get_value($site['dbname'].'_'.$channel['id'])){
continue;
}
$keyword_title_num = ReadIntFromStr($config['keyword_title_num']);
if($keyword_title_num>$sendnum) $keyword_title_num = $sendnum;
$keyword_titles = array();
for($i=0;$i<$keyword_title_num;$i++){
$num = rand(0,$sendnum);
if(!in_array($num, $keyword_titles)){
$keyword_titles[] = $num;
}else{
$i--;
continue;
}
}
$channel = array_map('GBKTOUTF8', $channel);
msg($channel['typename'].'将发布'.$sendnum.'篇文章');
for($i=$log->get_value($site['dbname'].'_'.$channel['id']);$i<$sendnum;$i++){
$txtfile = GetArtFile(DIR_TXTFILE);
if($txtfile){
if(in_array($i, $keyword_titles)){
$art = GetArtContent($channel['id'], $txtfile, $keylink, GBKTOUTF8($site_name));
}else{
$art = GetArtContent($channel['id'], $txtfile, $keylink);
}
if($art){
msg('正在向 ['.$channel['typename'].'] 中添加文章 《'.$art['title'].'》');
if($model->art_add($art)){
$backupfile=DIR_BACKUP.substr($txtfile, strlen(DIR_TXTFILE));
if(!file_exists(dirname($backupfile))) mkdirs(dirname($backupfile));
rename($txtfile, $backupfile);
msg('添加成功,备份txt文件');
$log->add($site['dbname'].'_'.$channel['id'], $i);
$log->save();
}else{
msg('文章添加失败');
}
}else{
msg('向 ['.$channel['typename'].'] 中添加文章失败,跳过');
continue;
}
}else{
msg('没有找到可用的txt文件');
break;
}
echo "\r\n";
}
$log->add($site['dbname'].'_'.$channel['id'], 'finish');
$log->save();
}
$log->add($site['dbname'], 'finish');
$log->save();
}else{
msg('没有找到文章栏目,跳过这个站点');
echo "\r\n";
}
5, 过多使用
@
抑制错误,不使用异常处理, 类的方法没有明确声明访问权限.
class db {
private $config = array(
'dbhost' => '',
'dbname' => '',
'dbuser' => '',
'dbpwd' => '',
'dbprefix' => '',
'dbcharset' => ''
);
public $linkID = null;
public $isconnect = FALSE;
function __construct($config) {
$this->config = $config + $this->config;
$this->connect();
}
function connect(){
if($this->config['dbhost'] && $this->config['dbname']){
$this->linkID = @mysql_connect($this->config['dbhost'], $this->config['dbuser'], $this->config['dbpwd']);
if(!$this->linkID){
//连接错误,直接退出
//echo mysql_error()."\r\n";
return FALSE;
}
if (@mysql_select_db($this->config['dbname'])) {
if($this->config['dbcharset'] != ''){
@mysql_query("SET NAMES '{$this->config['dbcharset']}'", $this->linkID);
}
}
$this->isconnect = TRUE;
}
return $this->isconnect;
}
}
太多了..不好弄...
安式回转跑
answered 11 years, 2 months ago