Home > Backend Development > PHP Tutorial > Some problems of PHP in large website development_PHP tutorial

Some problems of PHP in large website development_PHP tutorial

WBOY
Release: 2016-07-13 17:29:21
Original
834 people have browsed it

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

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

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

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

以下为引用的内容:
   /*
 * the three faces of π
 */

 printf ("pi is: %.2f
", m_pi);
   printf ("pi is also: %.3f
", m_pi);
   printf ("pi is also: %.4f
", m_pi);
  ?>

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

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

  在php中有以下几种基本变量:

  boolean, resource, integer, double, string, array and object。
  
  三、临时变量的使用

    临时变量的滥用会导致程序运行效率的降低。何时使用临时变量可基于以下两点考虑:

  1、该变量是否至少使用两次。

  2、该变量的使用是否会显著提高程序的可读性。

  如果一条也不满足,则省略该变量的使用。例如:

以下为引用的内容:
   $tmp = date ("f d, h:i a"); /* ie january 3, 2:30 pm */
 print $tmp;
?>

  就应该改成:
 

以下为引用的内容:
   print date ("f d, h:i a");
?>

  又如:

以下为引用的内容:
  // string reverse_characters(string str)
// reverse all of the characters in a string.
function reverse_characters ($str)
{
 return implode ("", array_reverse (preg_split("//", $str)));
}
?>

is not very readable, it can be changed to:

以下为引用的内容:
  // 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);
}
?>

4. Separation of client and server code

The client-side and server-side codes in the PHP program are actually HTML code and PHP language code. Many people mix HTML and PHP statements in one file, making the file very large. This style reduces the maintenance of the program. It is very unfavorable for development and redevelopment and is not suitable for the development of large-scale sites. There are generally two ways to separate html and php statements:

1. Write a dedicated API, such as:

index.php? the client side

以下为引用的内容:
 
 
 
  <?php print_header (); ?>
 
 
 


 
 
 
   
 
 

 
 

 
 

 
 



The following is the quoted content:

TABLE>

site.lib? the server side code






The following is the quoted content:

$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; 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);
http://www.bkjia.com/PHPjc/531681.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531681.htmlTechArticlephp has been rapidly promoted for its ease of use, but ease of use does not mean that you can use it well. In fact, Many programmers use it to easily build web application systems, but how many people carefully consider...
<?php print_header (); ?>










< ;/td>


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template