在php編程中,為自定義函數(shù)設定默認值,當用戶調(diào)用該函數(shù)時,如果不給參數(shù)指定值,參數(shù)會用默認值頂替,下面看例子。
例1,代碼如下:
- <html>
- <head>
- <title>php函數(shù)指定默認值-www.survivalescaperooms.com</title>
- </head>
- <body>
- <?php
- function printMe($param = NULL)
- {
- print $param;
- }
- printMe("This is test");
- printMe();
- ?>
- </body>
- </html>
輸出結(jié)果:
This is test
例2 php函數(shù)參數(shù)默認值的使用范例,php函數(shù)參數(shù)中設置和使用默認值,代碼如下:
- <html>
- <head>
- <title>php函數(shù)參數(shù)默認值 - www.survivalescaperooms.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>
新聞熱點
疑難解答