问:因为@ Prince在答复中指出了我的错误,所以我开始意识到我已经开始养成一些非常糟糕的编程习惯,例如嵌套循环(例如,N嵌套了多个条件),导致}符号积累,从而使代码质量很差可读的。以前,我们只关注函数的实现,而没有忽略代码的可读性和性能。
以下列出了一些我知道的不良编程习惯:
1.嵌套循环
2.过多的条件嵌套
3.用户提交数据而未过滤
4.函数体太长
5.变量的明确命名
我相信有很多。PHPer阅读了“ PHP最佳实践”后,也许我们可以集思广益,总结出“ PHP最糟糕的实践”,它也可以作为警告记录,提醒自己随时优化代码。PHP培养有经验的前辈不能任教。)
答: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.迟到不归,代码挤在一起
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)!='#'){
// Replace multiple spaces with one to increase the fault tolerance rate
$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 ('server'. $host.'join the publishing queue');
}else{
MSG ('server'. $host.'No time to release');
}
}
}
}
echo "\r\n";
}
fclose($fp);
return $hosts;
}
3.全局变量,以下msg函数被大量调用…
function SendToSite($site){
global $db,$log,$config;
if($log->get_value($site['dbname'])==='finish') return;
echo "=====================\r\n";
MSG ('Start Connecting to Database'. $site ['dbname']);
if($db->select_db($site['dbname'])){
MSG ('Connect Successfully, Collect Site Information');
if($db->HasTable('archives')){
MSG ('Find the article datasheet and start publishing');
}else{
MSG ('No article data table found, table prefix may be incorrect, skip this site');
return;
}
}else{
MSG ('Connection failed, skip this site');
return;
}
4.超长排料
if(count($site_channel)>0) {
$keylink = '<a href="'.$site_url.'" target="_blank">'.GBKTOUTF8($site_name).'</a>';
MSG ('Find'. count ($site_channel).'Articles'column');
echo "\r\n";
foreach ($site_channel as $channel){
if($log->get_value($site['dbname'].'_'.$channel['id'])==='finish'){
MSG ($site ['dbname'].'Column ID is'. $channel ['id'].'Published today, skip');
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'].'will be released'. $sendnum.'article');
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 ('adding the article'. $art ['title'].') to ['. $channel ['typename'].');
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 ('add successfully, backup TXT file');
$log->add($site['dbname'].'_'.$channel['id'], $i);
$log->save();
}else{
MSG ('article addition failed');
}
}else{
MSG ('Failed to add an article to ['. $channel ['typename'].'), skip');
continue;
}
}else{
MSG ('No available TXT file was found');
break;
}
echo "\r\n";
}
$log->add($site['dbname'].'_'.$channel['id'], 'finish');
$log->save();
}
$log->add($site['dbname'], 'finish');
$log->save();
}else{
MSG ('No article column found, skip this site');
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){
// Connection error, exit directly
//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;
}
}
使用警告
示例:
$arr = array('a'=>1, 'b'=>2);
foreach($arr as &$v)
{
if($v == '2')
{
$v = 3;
}
}
$v = 444;
将$ arr ['b']的值显示为444;
可以进行如下更改:
$arr = array('a'=>1, 'b'=>2);
foreach($arr as $k => $v)
{
if($v == '2')
{
$arr[$k] = 3;
}
}