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