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

[PHP中文社区教程] 第二十二课:自定义常量


2006-12-21 17:15:42 7,583 0 发表评论 字体: 作者:C.K.
标签: php中文社区教程

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

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

  还记得前面我们讲过的PHP中的常量吗?常量是命名了的常数(可以是任何类型的数据)。常量的使用与变量有较大的区别:首先常量不加美元符($),直接用名字来引用它就行了;其次常量的值不可改变,即不可重定义。例如22-1:

<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: #DD0000">&#39;我使用的PHP版本号:&#39;</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_VERSION</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//PHP_VERSION是PHP内置常量<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">&#39;运行PHP的操作系统:&#39;</span><span style="color: #007700">.</span><span style="color: #0000BB">PHP_OS</span><span style="color: #007700">;&nbsp;&nbsp;</span><span style="color: #FF8000">//PHP_OS是PHP内置常量<br /><br />&nbsp;<br /><br /></span><span style="color: #0000BB">?&gt;</span><br /></span><br />

  在PHP中我们还可以按需要自定义常量,方法是使用define()函数。常量常用来记录整个程序中不变的数据,而且常常放在程序的开头,这样做有利于日后程序的修改和调试。例如22-2:

<span style="color: #000000"><br /><span style="color: #0000BB">&lt;?php<br /><br />&nbsp;<br /><br />define</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"SITE"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"php中文社区"&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//定义常量SITE<br /><br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"URL"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"www.phpNet.cn"&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//定义常量URL<br /><br />&nbsp;<br /><br />&nbsp;<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"有个学习和讨论PHP的地方叫"</span><span style="color: #007700">.</span><span style="color: #0000BB">SITE</span><span style="color: #007700">;<br /><br />echo&nbsp;</span><span style="color: #DD0000">"它的网址是"</span><span style="color: #007700">.</span><span style="color: #0000BB">URL</span><span style="color: #007700">;<br /><br />&nbsp;<br /><br /></span><span style="color: #FF8000">/*<br /><br />结果输出:<br /><br />有个学习和讨论PHP的地方叫php中文社区<br /><br />它的网址是www.phpNet.cn<br /><br />*/<br /><br />&nbsp;<br /><br /></span><span style="color: #0000BB">?&gt;</span><br /></span><br />

你还可以用define()函数做些复杂的事情。例如22-3:

<span style="color: #000000"><br /><span style="color: #0000BB">&lt;?php<br /><br />&nbsp;<br /><br />define</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"TIYU"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">&lt;&lt;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//体育=2<br /><br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"CHANGGE"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">&lt;&lt;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//唱歌=4<br /><br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"MEISHU"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">&lt;&lt;</span><span style="color: #0000BB">3&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//美术=8<br /><br /></span><span style="color: #0000BB">define</span><span style="color: #007700">(&nbsp;</span><span style="color: #DD0000">"YUEDU"</span><span style="color: #007700">,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">&lt;&lt;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">);&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//阅读=16<br /><br />&nbsp;<br /><br /></span><span style="color: #0000BB">$A&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//客户A的爱好有“体育+美术”<br /><br /></span><span style="color: #0000BB">$B&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">8</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//客户B的爱好有“唱歌+美术”<br /><br /></span><span style="color: #0000BB">$C&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">4&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">16</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//客户C的爱好有“体育+唱歌+阅读”<br /><br />&nbsp;<br /><br />/*<br /><br />也可以这样定义客户爱好<br /><br />$A&nbsp;=&nbsp;TIYU&nbsp;|&nbsp;MEISHU;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//客户A的爱好有“体育+美术”<br /><br />$B&nbsp;=&nbsp;CHANGGE&nbsp;|&nbsp;MEISHU;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//客户B的爱好有“唱歌+美术”<br /><br />$C&nbsp;=&nbsp;TIYU&nbsp;|&nbsp;CHANGGE&nbsp;|&nbsp;YUEDU;&nbsp;&nbsp;&nbsp;&nbsp;//客户C的爱好有“体育+唱歌+阅读”<br /><br />*/<br /><br />&nbsp;<br /><br />//定义一个打印客户爱好的函数<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">aihao</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">){<br /><br />&nbsp;<br /><br />&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">&amp;&nbsp;</span><span style="color: #0000BB">TIYU&nbsp;</span><span style="color: #007700">)&nbsp;echo&nbsp;</span><span style="color: #DD0000">"体育&lt;br&gt;"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">&amp;&nbsp;</span><span style="color: #0000BB">CHANGGE&nbsp;</span><span style="color: #007700">)&nbsp;echo&nbsp;</span><span style="color: #DD0000">"唱歌&lt;br&gt;"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">&amp;&nbsp;</span><span style="color: #0000BB">MEISHU&nbsp;</span><span style="color: #007700">)&nbsp;echo&nbsp;</span><span style="color: #DD0000">"美术&lt;br&gt;"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;if(&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">&amp;&nbsp;</span><span style="color: #0000BB">YUEDU&nbsp;</span><span style="color: #007700">)&nbsp;echo&nbsp;</span><span style="color: #DD0000">"阅读&lt;br&gt;"</span><span style="color: #007700">;<br /><br />&nbsp;<br /><br />}<br /><br />&nbsp;<br /><br />echo&nbsp;</span><span style="color: #DD0000">&#39;客户A的爱好有:&lt;br&gt;&#39;</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">aihao</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$A&nbsp;</span><span style="color: #007700">);<br /><br />&nbsp;<br /><br />echo&nbsp;</span><span style="color: #DD0000">&#39;客户B的爱好有:&lt;br&gt;&#39;</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">aihao</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$B&nbsp;</span><span style="color: #007700">);<br /><br />&nbsp;<br /><br />echo&nbsp;</span><span style="color: #DD0000">&#39;客户C的爱好有:&lt;br&gt;&#39;</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">aihao</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$C&nbsp;</span><span style="color: #007700">);<br /><br />&nbsp;<br /><br /></span><span style="color: #0000BB">?&gt;</span><br /></span><br />

从上面的例子我们可以看出,常量是全局有效的,可以在函数内部引用。

网站统计 Statistics

  • 创建时间: 2005年1月3日 距今5094 天
  • 日志总数: 2461
  • 评论总数: 630
  • 标签总数: 654
  • 链接总数: 273
  • 最后更新: 2018-8-31 17:57:04
  • 您是本站第 14270407 位访客

广告区 Guǎng Gào