PHP学习笔记1-常量,函数

WBOY
发布: 2016-08-08 09:22:06
原创
930 人浏览过

常量:使用const(php5)声明,只能被赋值一次,php5以下版本使用define;

<span>1</span> <span>php
</span><span>2</span><span>const</span> THE_VALUE = 100;<span>//</span><span>PHP5中才有const</span><span>3</span><span>echo</span><span> THE_VALUE;
</span><span>4</span><span>5</span><span>define</span>('THE_VALUE',200);<span>//</span><span>低于PHP5的老版本可以使用define</span><span>6</span><span>echo</span> THE_VALUE;
登录后复制

函数:包装了很多功能的代码块;

优点:便于在别处调用

<span> 1</span> <span>php
</span><span> 2</span><span> 3</span><span>function</span><span> traceHelloPHP(){
</span><span> 4</span><span>echo</span> 'Hello PHP!'<span>;
</span><span> 5</span><span>echo</span> '<br>'<span>;
</span><span> 6</span><span>echo</span> 'Hello World!'<span>;
</span><span> 7</span><span>echo</span> '<br>'<span>;
</span><span> 8</span><span>}
</span><span> 9</span><span>traceHelloPHP();
</span><span>10</span><span>11</span><span>//</span><span>函数的另外一种执行方式</span><span>12</span><span>$func</span> = 'traceHelloPHP';<span>//</span><span>把函数当成参数传递,e.g.回调方法</span><span>13</span><span>$func</span><span>();
</span><span>14</span><span>15</span><span>//</span><span>函数的传入参数--单个传入参数</span><span>16</span><span>function</span> sayHelloTo(<span>$name</span><span>){
</span><span>17</span><span>echo</span> 'Hello '.<span>$name</span>.'<br>'<span>;
</span><span>18</span><span>}
</span><span>19</span> sayHelloTo('Vito'<span>);
</span><span>20</span><span>21</span><span>//</span><span>函数的传入参数--多个传入参数</span><span>22</span><span>function</span> traceNum(<span>$a</span>,<span>$b</span><span>){
</span><span>23</span><span>//</span><span>    echo 'a = '.$a.', b = '.$b.'<br>';</span><span>24</span><span>echo</span> "a = <span>$a</span>,b = <span>$b</span>";<span>//</span><span>较为简便的写法</span><span>25</span><span>}
</span><span>26</span> traceNum(2,3<span>);
</span><span>27</span><span>28</span><span>function</span> add(<span>$a</span>,<span>$b</span><span>){
</span><span>29</span><span>return</span><span>$a</span>+<span>$b</span>;<span>//</span><span>返回值</span><span>30</span><span>}
</span><span>31</span><span>echo</span> add(10,2);
登录后复制
View Code

以上就介绍了PHP学习笔记1-常量,函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
php
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!