PHP头条
热点:

PHP在大型网站开发中的一些问题


php以其易用性得到迅速的推广,但易用并不是说就能用好它,实际上许多程序员用它很容易的立一个个web应用系统,但又有多少人仔细的考虑过他们的代码,是否容易维护、是否足够健壮、否效率足够高、是否足够安全,当php用于建立大型网站时这些就成为很关键的因素。下面我们从较轻微的问题开始讨论,直至一些致命的错误。共分三部分。

  第一部分、较轻微的错误
  
  一、printf(),

    该函数主要用来格式化显示数据。当你要改变某个数据的显示格式时才使用。

  例如以不同的精度来显示pi(3.1415926)的值。

以下为引用的内容:
  <?php
   /*
   * the three faces of π
   */
  
   printf ("pi is: %.2f <br> ", m_pi);
   printf ("pi is also: %.3f <br> ", m_pi);
   printf ("pi is also: %.4f <br> ", m_pi);
  ?>

   但许多程序员仅仅为显示一些变量值和函数返回值使用该函数。因为printf()在显示数据前要先格式化该数据以速度较慢,因此,仅为了显示数据时应用print和echo,以提高速度。 字串6
  
  二、语意检查

  php是一种弱类型语言,也就是说在使用一个变量前不用定义,这样给编程带来了很大的方便和灵活,但你自己必须知道该变量到底应该是哪种类型,因为该变量在运行时仍实际对应着某一种类型(各种类型之间可以自由互相转换),没有类型的变量是不存在的。有可能php并不能检查出你的语意错误,但由于变量类型的变化,会导致一些潜在的问题的发生。另外一个值得注意的问题是变量的范围,它也可能会导致一些潜在的问题的发生。

  在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);
  }
  ?>

   四、客户端和服务器端代码的分离

    客户端和服务器端代码的在php程序中实际上就是html代码和php语言代码,很多人把html和php语句混合在一个文件里,使得这文件很大,这种风格对程序的维护和再开发很不利,不适合大型站点的开发。一般有两种方法把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 (" ", $site_info->links);
   $names = explode (" ", $site_info->link_names);
  
  for ($i = 0; $i < count ($links);

www.phpzy.comtrue/phprm/35981.htmlTechArticlePHP在大型网站开发中的一些问题 php以其易用性得到迅速的推广,但易用并不是说就能用好它,实际上许多程序员用它很容易的立一个个web应用系统,但又有多少人仔细的考虑过他们的代...

相关文章

    暂无相关文章

PHP之友评论

今天推荐