Home > php教程 > php手册 > body text

速战速决 (3) - PHP: 函数基础, 函数参数, 函数返回值, 可变函数, 匿名函数, 闭包函数, 回调函数 - webabcd

WBOY
Release: 2016-05-20 10:14:53
Original
2021 people have browsed it

[源码下载]


速战速决 (3) - PHP: 函数基础, 函数参数, 函数返回值, 可变函数, 匿名函数, 闭包函数, 回调函数



作者:webabcd


介绍
速战速决 之 PHP

  • 函数基础
  • 函数参数
  • 函数返回值
  • 可变函数
  • 匿名函数
  • 闭包函数
  • 回调函数



示例
1、函数的相关知识点 1(基础)
function/function1.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 函数的相关知识点 1(基础)
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">//</span><span style="color: #008000;"> 可以在相关的 function 声明语句之前调用该函数</span>
<span style="color: #000000;">f1();
</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> f1()
{
    </span><span style="color: #0000ff;">echo</span> "f1"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 这里调用 f2() 就错误了
// f2();</span>
<span style="color: #800080;">$b</span> = <span style="color: #0000ff;">true</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$b</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">function</span> f2() <span style="color: #008000;">//</span><span style="color: #008000;"> 条件成立才会声明此函数</span>
<span style="color: #000000;">    {
        </span><span style="color: #0000ff;">echo</span> "f2"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
    }
}
f2();


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 这里调用 f4() 就错误了(因为 f4 的声明是在 f3 的内部,所以要先调用 f3 才能再调用 f4)
// f4();</span>
<span style="color: #000000;">f3();
f4();
</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> f3()
{
    </span><span style="color: #0000ff;">echo</span> "f3"<span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

    </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> f4()
    {
        </span><span style="color: #0000ff;">echo</span> "f4"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
    }
}</span>
Copy after login


2、函数的相关知识点 2(函数参数)
function/function2.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 函数的相关知识点 2(函数参数)
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">//</span><span style="color: #008000;"> 带参数的函数</span>
f1("webabcd"<span style="color: #000000;">);
</span><span style="color: #0000ff;">function</span> f1(<span style="color: #800080;">$name</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">echo</span> "hello " . <span style="color: #800080;">$name</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: hello webabcd</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 带参数的函数(参数是引用类型)</span>
<span style="color: #800080;">$name</span> = "webabcd"<span style="color: #000000;">;
f2(</span><span style="color: #800080;">$name</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">function</span> f2(&<span style="color: #800080;">$n</span>) <span style="color: #008000;">//</span><span style="color: #008000;"> 参数是引用类型</span>
<span style="color: #000000;">{
    </span><span style="color: #0000ff;">echo</span> "hello " . <span style="color: #800080;">$n</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: hello webabcd</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;

    </span><span style="color: #800080;">$n</span> = "wanglei"; <span style="color: #008000;">//</span><span style="color: #008000;"> $n 是引用类型,其与全局变量 $name 指向的内容是一样的</span>
<span style="color: #000000;">}
</span><span style="color: #0000ff;">echo</span> "hello " . <span style="color: #800080;">$name</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: hello wanglei</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 带参数的函数(参数可以指定默认值)</span>
f3("hello "); <span style="color: #008000;">//</span><span style="color: #008000;"> output: hello webabcd</span>
f3("hello ", "wanglei"); <span style="color: #008000;">//</span><span style="color: #008000;"> output: hello wanglei</span>
<span style="color: #0000ff;">function</span> f3(<span style="color: #800080;">$p1</span>, <span style="color: #800080;">$p2</span> = "webabcd") <span style="color: #008000;">//</span><span style="color: #008000;"> 参数的默认值只能是常量,不能是表达式</span>
<span style="color: #000000;">{
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$p1</span> . <span style="color: #800080;">$p2</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}</span>
Copy after login


3、函数的相关知识点 3(函数返回值)
function/function3.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 函数的相关知识点 3(函数返回值)
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">//</span><span style="color: #008000;"> 带返回值的函数</span>
<span style="color: #800080;">$result1</span> = f1("webabcd"<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$result1</span><span style="color: #000000;">;
</span><span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">function</span> f1(<span style="color: #800080;">$name</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">return</span> "hello " . <span style="color: #800080;">$name</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: hello webabcd</span>
<span style="color: #000000;">}


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 从函数返回以一个引用(函数的声明和调用都要加 &)</span>
<span style="color: #800080;">$result2</span> = &<span style="color: #000000;">f2();
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$result2</span>;  <span style="color: #008000;">//</span><span style="color: #008000;"> output: 1</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #800080;">$result2</span> = 100; <span style="color: #008000;">//</span><span style="color: #008000;"> $result2 是个引用,指向 $i 所指向的内容,所以此句之后 $i 的值为 100</span>
<span style="color: #800080;">$result3</span> = &<span style="color: #000000;">f2();
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$result3</span>;  <span style="color: #008000;">//</span><span style="color: #008000;"> output: 101</span>
<span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
</span><span style="color: #0000ff;">function</span> &f2() <span style="color: #008000;">//</span><span style="color: #008000;"> 此函数会返回一个引用(函数的声明和调用都要加 &)</span>
<span style="color: #000000;">{
    </span><span style="color: #0000ff;">static</span> <span style="color: #800080;">$i</span> = 0<span style="color: #000000;">;
    </span><span style="color: #800080;">$i</span> ++<span style="color: #000000;">;

    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$i</span><span style="color: #000000;">;
}</span>
Copy after login


4、函数的相关知识点 4(可变函数,匿名函数,闭包函数)
function/function4.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 函数的相关知识点 4(可变函数,匿名函数,闭包函数)
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">//</span><span style="color: #008000;"> variable function(可变函数),将变量的值作为函数名</span>
<span style="color: #0000ff;">function</span><span style="color: #000000;"> f1()
{
    </span><span style="color: #0000ff;">echo</span> "f1"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: f1</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">function</span><span style="color: #000000;"> f2()
{
    </span><span style="color: #0000ff;">echo</span> "f2"; <span style="color: #008000;">//</span><span style="color: #008000;"> output: f2</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}
</span><span style="color: #800080;">$func</span> = 'f1'<span style="color: #000000;">;
</span><span style="color: #800080;">$func</span><span style="color: #000000;">();
</span><span style="color: #800080;">$func</span> = 'f2'<span style="color: #000000;">;
</span><span style="color: #800080;">$func</span><span style="color: #000000;">();


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 匿名函数</span>
<span style="color: #0000ff;">echo</span> <span style="color: #008080;">preg_replace_callback</span> <span style="color: #008000;">//</span><span style="color: #008000;"> output: userName</span>
<span style="color: #000000;">(
    </span>'~-([a-z])~',
    <span style="color: #0000ff;">function</span> (<span style="color: #800080;">$match</span>) <span style="color: #008000;">//</span><span style="color: #008000;"> 匿名函数</span>
<span style="color: #000000;">    {
        </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">strtoupper</span>(<span style="color: #800080;">$match</span>[1<span style="color: #000000;">]);
    }</span>,
    'user-name'<span style="color: #000000;">
);


</span><span style="color: #008000;">//</span><span style="color: #008000;"> 闭包函数
// $f3("webabcd"); // 这是错误的,对于闭包函数来说,调用时要在声明语句的后面</span>
<span style="color: #800080;">$f3</span> = <span style="color: #0000ff;">function</span>(<span style="color: #800080;">$name</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">echo</span> "hello " . <span style="color: #800080;">$name</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> output: hello webabcd</span>
    <span style="color: #0000ff;">echo</span> "<br>"<span style="color: #000000;">;
}; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 别忘了闭包函数的“;”</span>
<span style="color: #800080;">$f3</span>("webabcd");
Copy after login


5、函数的相关知识点 5(回调函数)
function/function5.php

<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 函数的相关知识点 5(回调函数)
 </span><span style="color: #008000;">*/</span>

<span style="color: #008000;">//</span><span style="color: #008000;"> 用于演示如何回调(callback)函数</span>
<span style="color: #0000ff;">function</span><span style="color: #000000;"> myCallbackFunction()
{
    </span><span style="color: #0000ff;">return</span> 'myCallbackFunction'<span style="color: #000000;">;
}

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> MyClass1
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 用于演示如何回调(callback)实例方法</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> myMethod1()
    {
        </span><span style="color: #0000ff;">echo</span> 'MyClass1 myMethod1'<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 用于演示如何回调(callback)类方法</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> myMethod2()
    {
        </span><span style="color: #0000ff;">echo</span> 'MyClass1 myMethod2'<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;
    }
}

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 call_user_func 调用指定的函数</span>
<span style="color: #0000ff;">echo</span> <span style="color: #008080;">call_user_func</span>('myCallbackFunction'<span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> '<br>'<span style="color: #000000;">;

</span><span style="color: #800080;">$obj1</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyClass1();
</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 call_user_func 调用指定对象的指定方法</span>
<span style="color: #008080;">call_user_func</span>(<span style="color: #0000ff;">array</span>(<span style="color: #800080;">$obj1</span>, 'myMethod1'<span style="color: #000000;">));

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 call_user_func 调用指类的类方法</span>
<span style="color: #008080;">call_user_func</span>(<span style="color: #0000ff;">array</span>('MyClass1', 'myMethod2'<span style="color: #000000;">));

</span><span style="color: #008000;">//</span><span style="color: #008000;"> 通过 call_user_func 调用指类的类方法(PHP 5.2.3 或以上)</span>
<span style="color: #008080;">call_user_func</span>('MyClass1::myMethod2');
Copy after login



OK
[源码下载]

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template