首页 » 建站技术 » PHP 技术 » 浏览内容

[PHP中文社区教程] 第十九课:自定义函数


2006-12-21 16:53:11 9,600 0 发表评论 字体: 作者:C.K.
标签: php中文社区教程

体验版 88 元,个人版 128 元,多用户版 288元个人版160元升级到多用户版。

From:http://www.phpnet.cn/bbs/viewthread.php?tid=555&extra=page%3D1

  函数功能强大,使用简单,我们只要按它的要求输入参数,它就能返回我们想要的结果。PHP中还能自定义函数,把那些在编程时要反复用到的功能块定义成一个函数,然后简单的调用。

  例如,我们经常要在网页里显示时间,而且时间格式各不一样,那么怎么把它定义成一个函数呢?又怎么调用呢?请看下面的代码19-1:

<span style="color: #000000"><br /><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//调用方法一<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">myDate</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /><br />&nbsp;<br /><br /></span><span style="color: #FF8000">//调用方法二<br /><br /></span><span style="color: #0000BB">$a</span><span style="color: #007700">=</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #0000BB">myDate</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /><br />&nbsp;<br /><br /></span><span style="color: #FF8000">//调用方法三<br /><br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">=</span><span style="color: #0000BB">6</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$dateStr</span><span style="color: #007700">=</span><span style="color: #0000BB">myDate</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$dateStr</span><span style="color: #007700">;<br /><br />&nbsp;<br /><br /></span><span style="color: #FF8000">//定义函数<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">myDate</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$style&nbsp;</span><span style="color: #007700">){<br /><br />&nbsp;&nbsp;switch(&nbsp;</span><span style="color: #0000BB">$style&nbsp;</span><span style="color: #007700">){<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y年m月d日"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"m/d/Y"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"y.m.d&nbsp;l"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M&nbsp;d&nbsp;Y"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">6&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y年第W周"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;default:&nbsp;break;<br /><br />&nbsp;&nbsp;}<br /><br />&nbsp;<br /><br />&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">;<br /><br />}<br /><br /></span><span style="color: #FF8000">//定义函数结束<br /><br /></span><span style="color: #0000BB">?&gt;</span><br /></span><br />

  上面的代码中,我们定义了一个函数myDate(),定义函数要由关键字 function 来声明,函数名是非大小写敏感的,$style是要输入日期样式的参数,函数内部就是实现特定功能的PHP代码,函数可以通过可选的return语句返回需要的值。函数定义之后,你可以象上例那样,在你需要的地方简单的调用。

  参数

  外部信息可以通过参数传递给函数中,参数就是一系列逗号分隔的变量或常量。PHP支持数值形参数,变量参数和默认参数。看一个默认参数使用的例子19-2:

<span style="color: #000000"><br /><span style="color: #0000BB">&lt;?php<br /><br />&nbsp;<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">myDate</span><span style="color: #007700">();<br /><br />&nbsp;<br /><br />function&nbsp;</span><span style="color: #0000BB">myDate</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$style</span><span style="color: #007700">=</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">){<br /><br />&nbsp;&nbsp;switch(&nbsp;</span><span style="color: #0000BB">$style&nbsp;</span><span style="color: #007700">){<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y年m月d日"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"m/d/Y"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"y.m.d&nbsp;l"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">5&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"M&nbsp;d&nbsp;Y"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">6&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y年第W周"</span><span style="color: #007700">);&nbsp;break;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;default:&nbsp;break;<br /><br />&nbsp;&nbsp;}<br /><br />&nbsp;<br /><br />&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">;<br /><br />}<br /><br />&nbsp;<br /><br /></span><span style="color: #0000BB">?&gt;</span><br /></span><br />

  上面的例子中,语句 echo myDate()并没有输入参数,但程序还是正确运行了,为什么呢?这是因为我们在定义函数时使用了默认参数$style=1,即在没有参数输入的情况下,函数就使用这个默认值。当有多个参数传递时,默认参数要写在最后。例如19-3:

1 2

    网站统计 Statistics

    • 创建时间: 2005年1月3日 距今4946 天
    • 日志总数: 2461
    • 评论总数: 630
    • 标签总数: 654
    • 链接总数: 273
    • 最后更新: 2017-7-1 18:16:33
    • 您是本站第 13717736 位访客

    广告区 Guǎng Gào