PHP 5:PHP文法導向

來源:互聯網
上載者:User
代碼
1 2
3 function do_html_header($title)
4 {
5 // print an HTML header
6 ?>
7
8
9 <?php echo $title;?>
10
16
17
18 19 align=left valign=bottom height = 55 width = 57 />
20

PHPbookmark


21
22 23 if($title)
24 do_html_heading($title);
25 }
26
27 function do_html_footer()
28 {
29 // print an HTML footer
30 ?>
31
32
33 34 }
35
36 function do_html_heading($heading)
37 {
38 // print heading
39 ?>
40


41 42 }
43
44 function do_html_URL($url, $name)
45 {
46 // output URL as link and br
47 ?>
48
">

49 50 }
51
52 function display_site_info()
53 {
54 // display some marketing info
55 ?>
56

    57
  • Store your bookmarks online with us!

  • 58
  • See what other users use?

  • 59
  • Share your favorite links with others?

  • 60

61 62 }
63
64 function display_login_form()
65 {
66 ?>
67 Not a member?
68
85 86 }
87
88 function display_registration_form()
89 {
90 ?>
91
111 112
113 }
114
115 function display_user_urls($url_array)
116 {
117 // display the table of URLs
118
119 // set global variable, so we can test later if this is on the page
120 global $bm_table;
121 $bm_table = true;
122 ?>
123

124
150 151 }
152
153 function display_user_menu()
154 {
155 // display the menu options on this page
156 ?>
157
158 Home |
159 Add BM |
160 161 // only offer the delete option if bookmark table is on this page
162 global $bm_table;
163 if($bm_table==true)
164 echo "Delete BM | ";
165 else
166 echo "Delete BM | ";
167 ?>
168 Change password
169

170 Recommend URLs to me |
171 Logout
172
173
174 175 }
176
177 function display_add_bm_form()
178 {
179 // display the form for people to ener a new bookmark in
180 ?>
181
188 189 }
190
191 function display_password_form()
192 {
193 // display html change password form
194 ?>
195

196
197










198 199 200 201 202 203 204 205 206 207 209
Old password:
New password:
Repeat new password:

208

210

211 212 };
213
214 function display_forgot_form()
215 {
216 // display HTML form to reset and email password
217 ?>
218

219
220




221 222 223 224 226
Enter your username

225

227

228 229 };
230
231 function display_recommended_urls($url_array)
232 {
233 // similar output to display_user_urls
234 // instead of displaying the users bookmarks, display recomendation
235 ?>
236 &nb#

本代碼也確實夠長的,但是仔細看看裡面的內容其實也沒有什麼,從介紹文法的角度來說,最好的方法就是選擇其中一部分代碼。這部分代碼足以概括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 13 $color = " #cccccc " ;
14 echo " Bookmark " ;
15 echo " Delete? " ;
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 " " . htmlspecialchars ( $url ) . " " ;
26 echo " 27 value=\ " $url \ " > " ;
28 echo " " ;
29 }
30 }
31 else
32 echo " No bookmarks on record " ;
33 ?>
34
35
36 37 }

OK,下面的描述將以本程式碼片段的行號為準。此函數用來顯示書籤資訊的。傳入的參數是書籤的數組。
看看上面的代碼。我將從以下幾個方面入手
PHP的基本類型 如何定義PHP的變數和常量 PHP的運算子 PHP的運算式 PHP的流程式控制制 PHP的函數 PHP中字串的操作 就這些,看起來還不少。
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特殊字元。
最後看看顯示的效果吧。
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.