速战速决 (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>
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>
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>
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");
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');
OK
[源码下载]
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
4 weeks agoByDDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
1 months agoByDDD
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
