在php編程中,為自定義函數設定默認值,當用戶調用該函數時,如果不給參數指定值,參數會用默認值頂替,下面看例子。
例1,代碼如下:
- <html>
- <head>
- <title>php函數指定默認值-www.companysz.com</title>
- </head>
- <body>
- <?php
- function printMe($param = NULL)
- {
- print $param;
- }
- printMe("This is test");
- printMe();
- ?>
- </body>
- </html>
輸出結果:
This is test
例2 php函數參數默認值的使用范例,php函數參數中設置和使用默認值,代碼如下:
- <html>
- <head>
- <title>php函數參數默認值 - www.companysz.com</title>
- </head>
- <body>
- <?php
- function textonweb ($content, $fontsize=3){
- echo "<font SIZE=$fontsize>$content</font>";
- }
- textonweb ("A <br />", 7);
- textonweb ("AA.<br />");
- textonweb ("AAA. <br />");
- textonweb ("AAAA! <br />");
- ?>
- </body>
- </html>
新聞熱點
疑難解答