PHP 5:PHP语法导向
2024-08-05 22:29:32 230人阅读
原文:PHP 5:PHP语法导向
代码 1 <? php 2 3 function do_html_header( $title ) 4 { 5 // print an HTML header 6 ?> 7 < html > 8 < head > 9 < title ><? php echo $title ; ?></ title > 10 < style > 11 body { font - family : Arial , Helvetica , sans - serif; font - size : 13px } 12 li , td { font - family : Arial , Helvetica , sans - serif; font - size : 13px } 13 hr { color : # 3333cc; width=300; text-align=left} 14 a { color : # 000000 } 15 </ style > 16 </ head > 17 < body > 18 < img src = " bookmark.gif " alt = " PHPbookmark logo " border = 0 19 align = left valign = bottom height = 55 width = 57 /> 20 < h1 >& nbsp;PHPbookmark </ h1 > 21 < hr /> 22 <? php 23 if ( $title ) 24 do_html_heading( $title ); 25 } 26 27 function do_html_footer() 28 { 29 // print an HTML footer 30 ?> 31 </ body > 32 </ html > 33 <? php 34 } 35 36 function do_html_heading( $heading ) 37 { 38 // print heading 39 ?> 40 < h2 ><? php echo $heading ; ?></ h2 > 41 <? php 42 } 43 44 function do_html_URL( $url , $name ) 45 { 46 // output URL as link and br 47 ?> 48 < br />< a href = " <?php echo $url;?> " ><? php echo $name ; ?></ a >< br /> 49 <? php 50 } 51 52 function display_site_info() 53 { 54 // display some marketing info 55 ?> 56 < ul > 57 < li > Store your bookmarks online with us !</ li > 58 < li > See what other users use ?</ li > 59 < li > Share your favorite links with others ?</ li > 60 </ ul > 61 <? php 62 } 63 64 function display_login_form() 65 { 66 ?> 67 < a href = ‘ register_form.php ‘ > Not a member ?</ a > 68 < form method = ‘ post ‘ action = ‘ member.php ‘ > 69 < table bgcolor = ‘ #cccccc ‘ > 70 < tr > 71 < td colspan = 2 > Members log in here ?</ td > 72 < tr > 73 < td > Username :</ td > 74 < td >< input type = ‘ text ‘ name = ‘ username ‘ ></ td ></ tr > 75 < tr > 76 < td > Password :</ td > 77 < td >< input type = ‘ password ‘ name = ‘ passwd ‘ ></ td ></ tr > 78 < tr > 79 < td colspan = 2 align = ‘ center ‘ > 80 < input type = ‘ submit ‘ value = ‘ Log in ‘ ></ td ></ tr > 81 < tr > 82 < td colspan = 2 >< a href = ‘ forgot_form.php ‘ > Forgot your password ?</ a ></ td > 83 </ tr > 84 </ table ></ form > 85 <? php 86 } 87 88 function display_registration_form() 89 { 90 ?> 91 < form method = ‘ post ‘ action = ‘ register_new.php ‘ > 92 < table bgcolor = ‘ #cccccc ‘ > 93 < tr > 94 < td > Email address :</ td > 95 < td >< input type = ‘ text ‘ name = ‘ email ‘ size = 30 maxlength = 100 ></ td ></ tr > 96 < tr > 97 < td > Preferred username < br /> ( max 16 chars) :</ td > 98 < td valign = ‘ top ‘ >< input type = ‘ text ‘ name = ‘ username ‘ 99 size = 16 maxlength = 16 ></ td ></ tr > 100 < tr > 101 < td > Password < br /> (between 6 and 16 chars) :</ td > 102 < td valign = ‘ top ‘ >< input type = ‘ password ‘ name = ‘ passwd ‘ 103 size = 16 maxlength = 16 ></ td ></ tr > 104 < tr > 105 < td > Confirm password :</ td > 106 < td >< input type = ‘ password ‘ name = ‘ passwd2 ‘ size = 16 maxlength = 16 ></ td ></ tr > 107 < tr > 108 < td colspan = 2 align = ‘ center ‘ > 109 < input type = ‘ submit ‘ value = ‘ Register ‘ ></ td ></ tr > 110 </ table ></ form > 111 <? php 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 < br /> 124 < form name = ‘ bm_table ‘ action = ‘ delete_bms.php ‘ method = ‘ post ‘ > 125 < table width = 300 cellpadding = 2 cellspacing = 0 > 126 <? php 127 $color = " #cccccc " ; 128 echo " <tr bgcolor=‘$color‘><td><strong>Bookmark</strong></td> " ; 129 echo " <td><strong>Delete?</strong></td></tr> " ; 130 if ( is_array ( $url_array ) && count ( $url_array ) > 0 ) 131 { 132 foreach ( $url_array as $url ) 133 { 134 if ( $color == " #cccccc " ) 135 $color = " #ffffff " ; 136 else 137 $color = " #cccccc " ; 138 // remember to call htmlspecialchars() when we are displaying user data 139 echo " <tr bgcolor=‘$color‘><td><a href=http://www.mamicode.com/ " $url \ " > " . htmlspecialchars ( $url ) . " </a></td> " ; 140 echo " <td><input type=‘checkbox‘ name=\ " del_me[]\ " 141 value=http://www.mamicode.com/ " $url \ " ></td> " ; 142 echo " </tr> " ; 143 } 144 } 145 else 146 echo " <tr><td>No bookmarks on record</td></tr> " ; 147 ?> 148 </ table > 149 </ form > 150 <? php 151 } 152 153 function display_user_menu() 154 { 155 // display the menu options on this page 156 ?> 157 < hr /> 158 < a href = " member.php " > Home </ a > & nbsp; |& nbsp; 159 < a href = " add_bm_form.php " > Add BM </ a > & nbsp; |& nbsp; 160 <? php 161 // only offer the delete option if bookmark table is on this page 162 global $bm_table ; 163 if ( $bm_table == true ) 164 echo " <a href=http://www.mamicode.com/‘#‘ onClick=‘bm_table.submit();‘>Delete BM | " ; 165 else 166 echo " <font color=‘#cccccc‘>Delete BM</font> | " ; 167 ?> 168 < a href = " change_passwd_form.php " > Change password </ a > 169 < br /> 170 < a href = " recommend.php " > Recommend URLs to me </ a > & nbsp; |& nbsp; 171 < a href = " logout.php " > Logout </ a > 172 < hr /> 173 174 <? php 175 } 176 177 function display_add_bm_form() 178 { 179 // display the form for people to ener a new bookmark in 180 ?> 181 < form name = ‘ bm_table ‘ action = ‘ add_bms.php ‘ method = ‘ post ‘ > 182 < table width = 250 cellpadding = 2 cellspacing = 0 bgcolor = ‘ #cccccc ‘ > 183 < tr >< td > New BM :</ td >< td >< input type = ‘ text ‘ name = ‘ new_url ‘ value = " http:// " 184 size = 30 maxlength = 255 ></ td ></ tr > 185 < tr >< td colspan = 2 align = ‘ center ‘ >< input type = ‘ submit ‘ value = ‘ Add bookmark ‘ ></ td ></ tr > 186 </ table > 187 </ form > 188 <? php 189 } 190 191 function display_password_form() 192 { 193 // display html change password form 194 ?> 195 < br /> 196 < form action = ‘ change_passwd.php ‘ method = ‘ post ‘ > 197 < table width = 250 cellpadding = 2 cellspacing = 0 bgcolor = ‘ #cccccc ‘ > 198 < tr >< td > Old password :</ td > 199 < td >< input type = ‘ password ‘ name = ‘ old_passwd ‘ size = 16 maxlength = 16 ></ td > 200 </ tr > 201 < tr >< td > New password :</ td > 202 < td >< input type = ‘ password ‘ name = ‘ new_passwd ‘ size = 16 maxlength = 16 ></ td > 203 </ tr > 204 < tr >< td > Repeat new password :</ td > 205 < td >< input type = ‘ password ‘ name = ‘ new_passwd2 ‘ size = 16 maxlength = 16 ></ td > 206 </ tr > 207 < tr >< td colspan = 2 align = ‘ center ‘ >< input type = ‘ submit ‘ value = ‘ Change password ‘ > 208 </ td ></ tr > 209 </ table > 210 < br /> 211 <? php 212 }; 213 214 function display_forgot_form() 215 { 216 // display HTML form to reset and email password 217 ?> 218 < br /> 219 < form action = ‘ forgot_passwd.php ‘ method = ‘ post ‘ > 220 < table width = 250 cellpadding = 2 cellspacing = 0 bgcolor = ‘ #cccccc ‘ > 221 < tr >< td > Enter your username </ td > 222 < td >< input type = ‘ text ‘ name = ‘ username ‘ size = 16 maxlength = 16 ></ td > 223 </ tr > 224 < tr >< td colspan = 2 align = ‘ center ‘ >< input type = ‘ submit ‘ value = ‘ Change password ‘ > 225 </ td ></ tr > 226 </ table > 227 < br /> 228 <? php 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 <? 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=http://www.mamicode.com/ " $url \ " > " . htmlspecialchars ( $url ) . " </a></td> " ; 26 echo " <td><input type=‘checkbox‘ name=\ " del_me[]\ " 27 value=http://www.mamicode.com/ " $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,下面的描述将以本代码段的行号为准。此函数用来显示书签信息的。传入的参数是书签的数组。 看看上面的代码。我将从以下几个方面入手 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特殊字符。 最后看看显示的效果吧。PHP 5:PHP语法导向
style blog http io ar
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉:
投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。
×
https://www.u72.net/daima/nbbhx.html