Home >Backend Development >PHP Tutorial >PHP study notes 1-constants, functions

PHP study notes 1-constants, functions

WBOY
WBOYOriginal
2016-08-08 09:22:06943browse

Constant: declared using const (php5), it can only be assigned once. Versions below php5 use 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;

Function: a code block that wraps many functions;

Advantage: easy to call elsewhere

<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

The above has introduced PHP study notes 1-constants, functions, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Merge two sorted arraysNext article:Merge two sorted arrays