標籤:cti body 出錯 個人 學習 自動調用 文章 自動 不同
ps:俺的文章俺懂就行。大家不要拿來學習不然每個人的學習思路不一樣。看視屏文章的老師不同筆記不同加上我也是新手上路,還怕誤導!請大家看行且思考,幫忙挖Bug也可以。
案例要求:你可以寫一個兩行三列的表格嗎?N行N列呢?
我們先來使用php寫一個簡單的兩行三列的表格。(PS:由此引出函數參數的作用)
<?php /**在寫的時候建議是成對的寫。*比如: for ($i=1; $i <=2; $i++) { $table.="<tr>"; //寫完立馬寫另外一個。以免漏泄或者其他未知的意外。 $table.="</tr>"; //點代表承接。 }** */$table = "<table border=‘1‘ cellpading=‘2‘>"; for ($i=1; $i <=2 ; $i++) { $table .= "<tr>"; for ($j=1; $j <=3 ; $j++) { $table .= "<td>test</td>"; } $table .= "</tr>"; }$table .="</table>";echo $table; ?>
輸出效果如下所示:
| test |
test |
test |
| test |
test |
test |
然後我們自訂一個函數,然後將$table放到裡面去。
<?php function createtable(){ $table = "<table border=‘1‘ cellpading=‘2‘>"; for ($i=1; $i <=2 ; $i++) { $table .= "<tr>"; for ($j=1; $j <=3 ; $j++) { $table .= "<td>test</td>"; } $table .= "</tr>"; } $table .="</table>"; echo $table;} ?>
這樣肯定是不行的。那麼我們來研究一下如何將這個表格設定成函數。這時候就是參數出場的時候了。
方法:function test($x,$y);
$x和$y就是參數,可有可無,可無限制。
<?php function createtable($x,$y){ $table = "<table border=‘1‘ cellpading=‘2‘>"; for ($i=1; $i <=$x ; $i++) { #將此處本來的2改為了$x,就會自動調用函數裡的。 $table .= "<tr>"; for ($j=1; $j <=$y ; $j++) { #將此處本來的3改為了$y $table .= "<td>test</td>"; } $table .= "</tr>"; } $table .="</table>"; echo $table;}createtable(2,3); #調用函數,記住哦,兩個參數就要有兩個值。否則會出錯呢! ?>
行了。九點半了學習PHP時間到了。
跟著百度學PHP[4]函數篇1-參數