跟著百度學PHP[4]函數篇1-參數

來源:互聯網
上載者:User

標籤: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-參數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.