|
<br> PHP以其易用性得到迅速的推广,但易用并不是说就能用好它,实际上许多程序员用它很容易的立一个个WEB应用系统,但又有多少人仔细的考虑过他们的代码,是否容易维护、是否足够健壮、否效率足够高、是否足够安全,当PHP用于建立大型网站时这些就成为很关键的因素。下面我们从较轻微的问题开始讨论,直至一些致命的错误。共分三部分。 <BR>第一部分、较轻微的错误 <BR><BR>一、Printf(), <BR> 该函数主要用来格式化显示数据。当你要改变某个数据的显示格式时才使用。 <BR>例如以不同的精度来显示PI(3.1415926)的值。 <BR><?php <BR> /* <BR> * The three faces of Π <BR> */ <BR><BR> printf (" i is: %.2f\n<br>\n", M_PI); <BR> printf (" i is also: %.3f\n<br>\n", M_PI); <BR> printf (" i is also: %.4f\n<br>\n", M_PI); <BR>?> <BR><BR> 但许多程序员仅仅为显示一些变量值和函数返回值使用该函数。因为Printf()在显示数据前要先格式化该数据以速度较慢,因此,仅为了显示数据时应用print和echo,以提高速度。 <BR><BR>二、语意检查 <BR> PHP是一种弱类型语言,也就是说在使用一个变量前不用定义,这样给编程带来了很大的方便和灵活,但你自己必须知道该变量到底应该是哪种类型,因为该变量在运行时仍实际对应着某一种类型(各种类型之间可以自由互相转换),没有类型的变量是不存在的。有可能PHP并不能检查出你的语意错误,但由于变量类型的变化,会导致一些潜在的问题的发生。另外一个值得注意的问题是变量的范围,它也可能会导致一些潜在的问题的发生。 <BR>在PHP中有以下几种基本变量: <BR>Boolean, resource, integer, double, string, array and object。 <BR><BR>三、临时变量的使用 <BR> 临时变量的滥用会导致程序运行效率的降低。何时使用临时变量可基于以下两点考虑: <BR>1、该变量是否至少使用两次。 <BR>2、该变量的使用是否会显著提高程序的可读性。 <BR>如果一条也不满足,则省略该变量的使用。例如: <BR><?php <BR> $tmp = date ("F d, h:i a"); /* ie January 3, 2:30 pm */ <BR> print $tmp; <BR>?> <BR>就应该改成: <BR><?php <BR> print date ("F d, h:i a"); <BR>?> <BR><BR>又如: <BR><?php <BR><BR>// string reverse_characters(string str) <BR>// Reverse all of the characters in a string. <BR>function reverse_characters ($str) <BR>{ <BR> return implode ("", array_reverse (preg_split("//", $str))); <BR>} <BR><BR>?> <BR>的可读性不强,可改成: <BR><?php <BR><BR>// string reverse_characters(string str) <BR>// Reverse all of the characters in a string. <BR>function reverse_characters ($str) <BR>{ <BR> $characters = preg_split ("//", $str); <BR> $characters = array_reverse ($characters); <BR><BR> return implode ("", $characters); <BR>} <BR><BR>?> <BR><BR>四、客户端和服务器端代码的分离 <BR> 客户端和服务器端代码的在PHP程序中实际上就是HTML代码和PHP语言代码,很多人把HTML和PHP语句混合在一个文件里,使得这文件很大,这种风格对程序的维护和再开发很不利,不适合大型站点的开发。一般有两种方法把HTML和PHP语句分开: <BR>1、编写专用API,例如: <BR><BR>index.php ? The Client side <BR><?php include_once ("site.lib"); ?> <BR><html> <BR><head> <BR><title> <?php print_header (); ?> </title> <BR></head> <BR><body> <BR><h1> <?php print_header (); ?> </h1> <BR><table border="0" cellpadding="0" cellspacing="0"> <BR><tr> <BR><td width="25%"> <BR><?php print_links (); ?> <BR></td> <BR><td> <BR><?php print_body (); ?> <BR></td> <BR></tr> <BR></table> <BR></body> <BR></html> <BR><BR><BR>site.lib ? The server side code <BR><BR><BR><?php <BR><BR>$dbh = mysql_connect ("localhost", "sh", "pass") <BR>or die (sprintf ("Cannot connect to MySQL [%s]: %s", <BR>mysql_errno (), mysql_error ())); <BR>@mysql_select_db ("MainSite") <BR>or die (sprintf ("Cannot select database [%s]: %s", <BR>mysql_errno (), mysql_error ())); <BR><BR>$sth = @mysql_query ("SELECT * FROM site", $dbh) <BR>or die (sprintf ("Cannot execute query [%s]: %s", <BR>mysql_errno (), mysql_error ())); <BR><BR>$site_info = mysql_fetch_object ($sth); <BR><BR>function print_header () <BR>{ <BR> global $site_info; <BR> print $site_info->header; <BR>} <BR><BR>function print_body () <BR>{ <BR> global $site_info; <BR> print nl2br ($site_info->body); <BR>} <BR><BR>function print_links () <BR>{ <BR> global $site_info; <BR><BR> $links = explode ("\n", $site_info->links); <BR> $names = explode ("\n", $site_info->link_names); <BR><BR>for ($i = 0; $i < count ($links); $i++) <BR>{ <BR> print "\t\t\t <BR> <a href=\"$links[$i]\">$names[$i]</a> <BR> \n<br>\n"; <BR>} <BR>} <BR>?> <BR><BR>这种方法使得程序看起来比较简洁,而且执行速度也较快。 <BR><BR>2、使用模板的方法 <BR>这种方法使得程序看起来更简洁,同样实现上面的功能,可用以下代码: <BR><html> <BR><head> <BR><title>%%PAGE_TITLE%%</title> <BR></head> <BR><body %%BODY_PROPERTIES%%> <BR><h1>%%PAGE_TITLE%%</h1> <BR><table border="0" cellpadding="0" cellspacing="0"> <BR><tr> <BR><td width="25%">%%PAGE_LINKS%%</td> <BR><td>%%PAGE_CONTENT%%</td> <BR></tr> <BR></table> <BR></body> <BR></html> <BR><BR> 用占位符代替要动态生成的内容,然后用一解析程序分析该模板文件,把占位符用际的内容替换。种方法使得即使不会使用PHP的页面制作人员也能修改模板文件。这种方法的缺点是执行效率不高,因为要解释模板文件。同时实现起来也比较复杂。 <BR><BR>注: www.thewebmasters.net的 FastTemplate class可方便的实现以上功能。 <BR><BR>五、不要用过时的函数 <BR>作为一种自由软件,PHP发展很快,其中的很多函数都已过时,例如: <BR><BR>while (1): <BR>print "5"; <BR>if ($idx++ == 5): <BR>break; <BR>endif; <BR>endwhile; <BR><BR> 虽然还能用但效率肯定不高,而且可能在以后的版本中会禁用,导致程序不能运行。因此要经常对照最新PHP手册检查那些函数已过时及时修正。 <br><br> |
|