首页 > 语言 > PHP > 正文

php函数指定默认值方法的小例子

2024-09-04 11:49:54
字体:
来源:转载
供稿:网友

php函数指定默认值

在php编程中,为自定义函数设定默认值,当用户调用该函数时,如果不给参数指定值,参数会用默认值顶替。

例1,代码如下:

  1. <html> 
  2. <head> 
  3. <title>php函数指定默认值-www.Vevb.com</title> 
  4. </head> 
  5. <body> 
  6. <?php 
  7. function printMe($param = NULL) 
  8.    print $param
  9. printMe("This is test"); 
  10. printMe(); 
  11. ?> 
  12. </body> 
  13. </html> 

输出结果:

This is test

例2,php函数参数默认值的使用范例,php函数参数中设置和使用默认值。

代码如下:

  1. <html> 
  2.   <head> 
  3.   <title>php函数参数默认值 - www.Vevb.com</title> 
  4.   </head> 
  5.   <body> 
  6.   <?php 
  7.   function textonweb ($content$fontsize=3){ 
  8.      echo "<font SIZE=$fontsize>$content</font>"
  9.   } 
  10.   textonweb ("A <br />", 7); 
  11.   textonweb ("AA.<br />"); 
  12.   textonweb ("AAA. <br />"); 
  13.   textonweb ("AAAA! <br />"); 
  14.   ?> 
  15.   </body> 
  16.   </html> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表