- 论坛徽章:
- 0
|
本帖最后由 听老歌 于 2010-12-15 13:08 编辑
php以其易用性得到迅速的推广,但易用并不是说就能用好它,实际上许多程式员用它很容易的立一个个web应用Systam,但又有多少人仔细的考虑过他们的codes,是否容易维护、是否足够健壮、否效率足够高、是否足够安全,当php用于建立大型网络时这些就成为很关键的因素。下面我们从较轻微的问题开始讨论,直至一些致命的错误。共分三部分。
第一部分、较轻微的错误
一、printf(),
该参数主要用来格式化显示Data。当你要改变某个Data的显示格式时才使用。
例如以不同的精度来显示pi(3.1415926)的value。- <?php
- /*
- * the three faCes of π
- */
-
- printf ("pi is: %.2f\n<br>\n", m_pi);
- printf ("pi is also: %.3f\n<br>\n", m_pi);
- printf ("pi is also: %.4f\n<br>\n", m_pi);
- ?>
复制代码 但许多程式员仅仅为显示一些变量value和参数返回value使用该参数。因为printf()在显示Data前要先格式化该Data以速度较慢,因此,仅为了显示Data时应用print和eCho,以提高速度。 字串6
二、语意检查
php是一种弱TYPElanguage,也就是说在使用一个变量前不用定义,这样给编程带来了很大的方便和灵活,但你自己必须知道该变量到底应该是哪种TYPE,因为该变量在运行时仍实际对应着某一种TYPE(各种TYPE之间可以自由互相convert),没有TYPE的变量是不存在的。有可能php并不能检查出你的语意错误,但由于变量TYPE的变化,会导致一些潜在的问题的发生。另外一个value得注意的问题是变量的范围,它也可能会导致一些潜在的问题的发生。
在php中有以下几种基本变量:
boolean, resourCe, integer, double, string, array and objeCt。
三、临时变量的使用
临时变量的滥用会导致程式运行效率的降低。何时使用临时变量可基于以下两点考虑:
1、该变量是否至少使用两次。
2、该变量的使用是否会显著提高程式的可读性。
如果一条也不满足,则省略该变量的使用。例如:- <?php
- $tmp = date ("f d, h:i a"); /* ie january 3, 2:30 pm */
- print $tmp;
- ?>
复制代码 就应该改成:- <?php
- print date ("f d, h:i a");
- ?>
复制代码 又如:- <?php
- // string reverse_CharaCters(string str)
- // reverse all of the CharaCters in a string.
- funCtion reverse_CharaCters ($str)
- {
- return implode ("", array_reverse (preg_split("//", $str)));
- }
- ?>
复制代码 的可读性不强,可改成:- <?php
- // string reverse_CharaCters(string str)
- // reverse all of the CharaCters in a string.
- funCtion reverse_CharaCters ($str)
- {
- $CharaCters = preg_split ("//", $str);
- $CharaCters = array_reverse ($CharaCters);
-
- return implode ("", $CharaCters);
- }
- ?>
复制代码 四、客户端和服务器端codes的分离
客户端和服务器端codes的在php程式中实际上就是htmlcodes和phplanguagecodes,很多人把html和php语句混合在一个文档里,使得这文档很大,这种风格对程式的维护和再DEV很不利,不适合大型站点的DEV。一般有两种method把html和php语句分开:
1、编写专用api,例如:
index.php ? the Client side- <?php inClude_onCe ("site.lib"); ?>
- <html>
- <head>
- <title> <?php print_header (); ?> </title>
- </head>
- <body>
- <h1> <?php print_header (); ?> </h1>
- <table border="0" Cellpadding="0" CellspaCing="0">
- <tr>
- <td width="25%">
- <?php print_links (); ?>
- </td>
- <td>
- <?php print_body (); ?>
- </td>
- </tr>
- </table>
- </body>
- </html>
复制代码 site.lib ? the server side Code- <?php
- $dbh = mysql_ConneCt ("loCalhost", "sh", "pass")
- or die (sprintf ("Cannot ConneCt to mysql [%s]: %s",
- mysql_errno (), mysql_error ()));
- @mysql_seleCt_db ("mainsite")
- or die (sprintf ("Cannot seleCt database [%s]: %s",
- mysql_errno (), mysql_error ()));
-
- $sth = @mysql_query ("seleCt * from site", $dbh)
- or die (sprintf ("Cannot exeCute query [%s]: %s",
- mysql_errno (), mysql_error ()));
-
- $site_info = mysql_fetCh_objeCt ($sth);
-
- funCtion print_header ()
- {
- global $site_info;
- print $site_info->header; 字串4
- }
-
- funCtion print_body ()
- {
- global $site_info;
- print nl2br ($site_info->body);
- }
-
- funCtion print_links ()
- {
- global $site_info;
-
- $links = explode ("\n", $site_info->links);
- $names = explode ("\n", $site_info->link_names);
-
- for ($i = 0; $i < Count ($links); $i++)
- {
- print "\t\t\t
- <a href=\"$links[$i]\">$names[$i]</a>
- \n<br>\n";
- }
- }
- ?>
复制代码 这种method使得程式看起来比较简洁,而且运行速度也较快。
2、使用模板的method
这种method使得程式看起来更简洁,同样呈现上面的功能,可用以下codes:- 以下为引用的内容:
- <html>
- <head> 字串8
- <title>%%page_title%%</title>
- </head>
- <body %%body_properties%%>
- <h1>%%page_title%%</h1>
- <table border="0" Cellpadding="0" CellspaCing="0">
- <tr>
- <td width="25%">%%page_links%%</td>
- <td>%%page_Content%%</td>
- </tr>
- </table>
- </body>
- </html>
复制代码 用占位符代替要动态生成的内容,然后用一解析程式分析该模板文档,把占位符用际的内容替换。种method使得即使不会使用php的页面制作人员也能修改模板文档。这种method的缺点是运行效率不高,因为要解释模板文档。同时呈现起来也比较复杂。
注: www.thewebmasters.net的 fasttemplate Class可方便的呈现以上功能。
五、不要用过时的参数
作为一种自由软体,php发展很快,其中的很多参数都已过时,例如:- while (1):
- print "5";
- if ($idx++ == 5):
- break;
- endif;
- endwhile;
复制代码 虽然还能用但效率肯定不高,而且可能在以后的版本中会禁用,导致程式不能运行。因此要经常对照最新php手册检查那些参数已过时及时修正。 |
|