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學習者快速成長!