php函數(shù)指定默認(rèn)值
在php編程中,為自定義函數(shù)設(shè)定默認(rèn)值,當(dāng)用戶調(diào)用該函數(shù)時(shí),如果不給參數(shù)指定值,參數(shù)會用默認(rèn)值頂替。
例1,代碼如下:
- <html>
- <head>
- <title>php函數(shù)指定默認(rèn)值-www.companysz.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ù)默認(rèn)值的使用范例,php函數(shù)參數(shù)中設(shè)置和使用默認(rèn)值。
代碼如下:
- <html>
- <head>
- <title>php函數(shù)參數(shù)默認(rèn)值 - 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>
新聞熱點(diǎn)
疑難解答