PHP 5:PHP语法导向


代码
本代码也确实够长的,但是仔细看看里面的内容其实也没有什么,从介绍语法的角度来说,最好的方法就是选择其中一部分代码。这部分代码足以概括PHP的语法。选哪个为好呢?OK,就选择115行的display_user_urls函数,其代码如下:
 1 function display_user_urls($url_array)
 2 {
 3   // display the table of URLs
 4 
 5   // set global variable, so we can test later if this is on the page
 6   global $bm_table;
 7   $bm_table = true;
 8 ?>
 9   <br />
10   <form name='bm_table' action='delete_bms.php' method='post'>
11   <table width=300 cellpadding=2 cellspacing=0>
12   <?php
13   $color = "#cccccc";
14   echo "<tr bgcolor='$color'><td><strong>Bookmark</strong></td>";
15   echo "<td><strong>Delete?</strong></td></tr>";
16   if (is_array($url_array&& count($url_array)>0)
17   {
18     foreach ($url_array as $url)
19     {
20       if ($color == "#cccccc")
21         $color = "#ffffff";
22       else
23         $color = "#cccccc";
24       // remember to call htmlspecialchars() when we are displaying user data
25       echo "<tr bgcolor='$color'><td><a href=\"$url\">".htmlspecialchars($url)."</a></td>";
26       echo "<td><input type='checkbox' name=\"del_me[]\"
27              value=\"$url\"></td>";
28       echo "</tr>"
29     }
30   }
31   else
32     echo "<tr><td>No bookmarks on record</td></tr>";
33 ?>
34   </table> 
35   </form>
36 <?php
37 }
OK,下面的描述将以本代码段的行号为准。此函数用来显示书签信息的。传入的参数是书签的数组。
看看上面的代码。我将从以下几个方面入手
就这些,看起来还不少。
OK,说完了上面的PHP介绍,再看看上面的代码,是不是觉得很easy。要是仍然觉得有点晕,请原谅我的表达。你也可以和我联系,改进一下它们。嘿嘿。
让我们继续解释上面的内容吧。
先看第一行,
function display_user_urls($url_array)
它是用来定义一个函数,因为前面有关键字function,参数是一个URL的数组。
看看第6行,定义了一个全局变量 $bm_table,并设置为true。
第10,11行为
10 <form name='bm_table' action='delete_bms.php' method='post'>
11   <table width=300 cellpadding=2 cellspacing=0>
它定义了一个form,在form里包含一个表。这个表用来显示书签的。接着看,14,15用来显示表的Title的。背景色为"#cccccc";字体为粗体。这里你可以看到
<tr bgcolor='$color'>
$color是一个变量,在我们上面说的字符串的操作里,如何显示一个字符串以及显示字符串的规则--PHP会尽可能的识别变量,在这里就有体现。
看看判断语句
16   if (is_array($url_array&& count($url_array)>0)
is_array判断$url_array是否为数组。count获取$url_array数组的个数。
18行的代码
18 foreach ($url_array as $url)
遍历数组的每个值,然后显示在Table里。
需要注意的是这里调用了htmlspecialchars函数,此函数用来处理用户数据里的HTML特殊字符。
最后看看显示的效果吧。

posted @ 2006-08-07 21:37  张太国  阅读(2240)  评论(1编辑  收藏  举报