A-A+

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

2020年08月08日 我爱编程 暂无评论

php函数指定默认值

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

例1,代码如下:

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

输出结果:

This is test

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

代码如下:

  1. <html>
  2.   <head>
  3.   <title>php函数参数默认值 - www.phpfensi.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>

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录