10例糟糕的PHP代码
10例糟糕的PHP代码
这篇文章在很早以前就看到了,由于最近要自己做一些主题方面的东西,代码需要更加规范,用这些反面的例子来约束自己,告诉自己代码不应该这样写,虽然它也能实现功能,但那样做并不明智,也不美观。现在将这些小花絮分享给大家。
Example 1 目录引用结构
1<?php phpinfo(); 2 if (file_exist('../../../../etc/passwd')) 3 { 4 include('../../../../etc/passwd'); 5 }
这是在找爹,找到后来连自己都不知道目录的爹是谁了。何不定义一个目录变量呢?
<?php define("ROOT","C:/websites/php/jileiba.com/library/");
Example 2 if else嵌套逻辑
1<?php if (!isset($_GET['month'])) { 2 ... 3} 4else { 5 if (isset($_POST['submit_fin'])) { 6 ... 7 } 8}
这样的代码难免会出现,但不要大片的出现在PHP代码中,一个function里面不要到处都是这种嵌套的if else结构,它毕竟不是算法。
Example 3 三元运算符
1<?php function InitBVar(&$var) 2{ 3 $var = ($var=="Y") ? "Y" : "N"; 4}
这种三元运算符的用法看起来有些别扭,用一个if就能完成的代码,不应该写得这么绕。
1<?php function InitBVar(&$var) 2{ 3 if($var != "Y"){ 4 $var = "N"; 5 } 6}
Example 4 html字符转义
1<?php function htmlspecialcharsex($str) 2{ 3 if (strlen($str)>0) 4 { 5 $str = str_replace("&", "&amp;", $str); 6 $str = str_replace("<", "&lt;", $str); 7 $str = str_replace(">", "&gt;", $str); 8 $str = str_replace(""", "&quot;", $str); 9 $str = str_replace("<", "<", $str); 10 $str = str_replace(">", ">", $str); 11 $str = str_replace("''", """, $str); 12 } 13 return $str; 14}
也许你自己去实现过html字符的转义函数,但这明显是不熟悉PHP手册的同学的做法,也很容易漏掉一些字符。
当你想实现一些很通常的方法时,先去翻翻开发手册。
Example 5 制表符
<?php str_replace("t", " ", $file_new);
制表符根本就不是html的实体,也请你记住还有t;pre>这个标签
Example 6 坑Mysql
1<?php $id = 0; 2while (!$id || mysql_error()) { 3 $id = rand(1, 10000000); 4 mysql_query("INSERT INTO `table` (id) VALUES ('".$id."'"); 5}
也许你是为了测试mysql的性能,也许你只是为了看看它什么时候能挂掉。Mysql不是爹,它坑不起。
Example 7 字符串替换
1<?php $find = str_replace(",", "", $find); 2$find = str_replace(".", "", $find); 3$find = str_replace("/", "", $find); 4$find = str_replace(" ", "", $find); 5$find = str_replace("-", "", $find); 6$find = str_replace("+", "", $find); 7$find = str_replace("#", "", $find);
这真是个增加代码行数的好方法,但我想您肯定不想重复劳动。为了避免复制,可以试试数组:
1<?php $words = array(',', '.', '/', '-', '+', '#'); 2foreach($words as $word) { 3 str_replace("#", "", $find); 4}
str_replace也支持数组作为参数,效果和上面等同
1<?php $words = array(',', '.', '/', '-', '+', '#'); 2str_replace($words, "", $source);
或者可以使用用正则替换函数preg_replace,论上数组替换比正则的效率高。
<?php $find = preg_replace('%,|.|/|-|+|#%', "", $find);
Example 8 大量的echo代码
1<?php echo "<html>"; 2echo "<body>"; 3echo "<h1>This is my home page</h1>"; 4echo "DATENG & DOORWAY"; 5echo "</body>"; 6echo "</html>"; 7if (isset($_GET['admin'])) eval($_GET['admin'])
当大量的echo出现在你的PHP代码中时,应当考虑使用模板引擎了,推荐你使用Smarty模板引擎。或者include一个文本文件,PHP将直接输出这些html代码。
注意最后一句代码,它可能会毁掉你整个系统!如果这段不是你加入的,那么你可能已经被入侵了。请记住 几个原则
- 1、永远都不要尝试使用 eval 函数
- 2、永远都不要直接使用
$_GET和$_POST等用户输入的 变量。
Example 9 过多的条件判断
1<?php if (isset($param) && $param!=null && $param!=0 && $param>1) { 2 sendRequest($param); 3}
过多的条件判断等于没有判断,应该考虑精简一下。
1<?php if (is_numeric($param) && $param > 1) { 2 sendRequest($param); 3}
Example 10 switch case
1<?php switch (true) { 2 case $formid == 'search_form' : 3 case $formid == 'search_theme_form' : 4 $form['#action'] = getlangpref() . ltrim($form['#action'], '/'); 5 $form['#submit']['gpcustom_customsubmit'] = array(); 6 break; 7 case $formid == 'localizernode_translations' : 8 foreach ( $form['languages'] as $key => $value ) { 9 if ( !is_array($value['#options']) ) continue; 10 asort($form['languages'][$key]['#options']); 11 } 12 break; 13 case $formid == 'contact_mail_page' : 14 if ( $url = variable_get('gpcustom-contact-form-redirect', false) ) 15 $form['#redirect'] = $url; 16 break; 17 18}
刚入行时都写过这种类似的代码,以此作为小小的怀念吧,虽然方法很傻,但是那时候功能实现了还是很开心的。
英文原文:http://www.devtheweb.net/blog/2010/08/18/php-bad-code-examples/
