Rumah > pembangunan bahagian belakang > tutorial php > PHP学习笔记1-常量,函数

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

WBOY
Lepaskan: 2016-08-08 09:22:06
asal
954 orang telah melayarinya

常量:使用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;
Salin selepas log masuk

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

优点:便于在别处调用

<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);
Salin selepas log masuk
View Code

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

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan