Blogger Information
Blog 26
fans 0
comment 0
visits 18145
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php中的函数
雪~人胖胖
Original
619 people have browsed it

1. 函数语法

函数是实现代码复用的重要方式,在所有编程语言中均如此

  1. function 函数名称(类型: 参数列表): 返回值类型
  2. {
  3. // 函数体
  4. return 返回值;
  5. }
序号 名称 描述
1 function 声明函数
2 函数名称 符合 PHP 标识符命名规范,不区分大小写
2 参数列表 零个或多个接收外部传入到函数的变量
2 {... 创建出一个封闭的函数作用域
2 函数体 由零个可多个合法的 PHP 语句组成
2 return 值 将执行结果返回函数调用者[可选]
2 ...} 函数执行结束,如果没有return,则返回null

2. 类型

序号 类型 语法 描述
1 自定义函数 function getName(){...} 用户根据业务需求创建
2 系统函数 substr(), count()... 也叫预定义函数,不必声明直接调用
3 可变函数 $funcName(); 函数名使用变量表示
4 匿名函数 $f = function (){...} 也叫”闭包”或”函数表达式”,常用做回调处理

2.1 自定义函数

用户根据业务需求创建

  1. <?php
  2. //驼峰命名法getPayment
  3. //蛇形命名法get_payment
  4. function getPayment(float $money,int $amount){
  5. return $amount*$money;
  6. }
  7. echo '总金额:'.getPayment(500, 12) .'元<br>';

2.2 系统函数(预定义函数)

  1. //系统函数,自带的,不需自定义
  2. echo strpos('You like php , I like php too!' ,'php');

2.3 可变函数

  1. function payment(float $money, float $discount){
  2. echo $money*$discount;
  3. }
  4. //把函数存在变量里,用变量调用函数
  5. $funpay = 'payment';
  6. $funpay(3000,0.8);

2.4 匿名函数

  1. $discount = 0.7;
  2. //匿名函数调用全局变量在函数function后面用use调用
  3. $getPayment = function (float $money, int $sum) use($discount){
  4. $payment = $money*$sum;
  5. return $payment>=500 ? $payment*$discount : $payment;
  6. };
  7. echo $getPayment(1000,2);
  8. //如果父作用域也是一个函数
  9. $f = function($discount)
  10. { //子函数
  11. $getPayment = function (float $money, int $sum) use($discount){
  12. $payment = $money*$sum;
  13. return $payment>=500 ? $payment*$discount : $payment;
  14. };
  15. return $getPayment;
  16. };
  17. echo $f(0.5)(500,20);

3. 返回值

  • 函数必须要有返回值
  • 函数必须是遵守单值返回原则
序号 场景 描述
1 return 可以返回任何类型的值,包括函数类型
2 return 遇到}也会返回, 默认返回null
  • 如果需要返回多个值,可以通过以下手段
序号 返回值类型 描述
1 string 字符串拼接
2 array 数组
3 json JSON 字符串
4 serialize 序列化字符串

json 和序列化,使用时需要进行解码操作

3.1 字符串拼接

-适合处理大量 php+html 代码混合;
-返回格式为用户自定义,前端处理非常麻烦;

  1. function fun1(): string
  2. {
  3. $staus = 1;
  4. $message = '成功';
  5. return $staus .':'. $message;
  6. }
  7. echo fun1();

3.2 数组返回

  1. function fun2():array
  2. {
  3. return ['status'=>2,'message'=>'失败'];
  4. }
  5. //格式化打印
  6. printf('<pre>%s</pre>',print_r(fun2(),true));

3.3json 返回

-是一种使用 js 字面量的方式来表示数据,是一种轻量级别的通用的数据交换或者传输格式;

-json 本质上就是一个具有一定结构和格式的字符串,几乎所有编程语言都有他的接口;

  1. function fun3():string
  2. {
  3. return json_encode(['status'=>2,'message'=>'失败']);
  4. }
  5. echo fun3();
  6. //将json字符串解析为php对象和数组
  7. $var = fun3();
  8. print_r(json_decode($var,true)) ;

通过序列化返回(serialize)

  1. function fun4():string
  2. {
  3. return serialize(['status'=>2,'message'=>'失败']);
  4. }
  5. echo fun4();
  6. //反序列化
  7. $var = unserialize(fun4());
  8. printf('<pre>%s<pre>',print_r($var,true));

4. 参数

  • 调用者可以通过参数将数据传递到函数中
  • 参数是以逗号分隔的表达式列表
  • 参数按照从左到右的顺序求值

参数类型

序号 类型 描述
1 值参数 默认传参方式
2 引用参数 改变原始调用参数值
3 默认参数 调用时允许省略的参数
4 剩余参数 调用参数数量不确定

4.1 值传递

-在函数中改变了调用参数的值,但是原始调用参数并没有发生变化

  1. function demo1(float $arg): float
  2. {
  3. return $arg*=2;
  4. }
  5. $value = 100;
  6. echo demo1($value);
  7. echo $value;
  8. //打印的函数值是200 $value的值也是100;

4.2 引用传递

用&符号

  1. function demo2(float &$arg): float
  2. {
  3. return $arg*=2;
  4. }
  5. $value = 100;
  6. echo demo2($value);
  7. echo $value;
  8. //打印的函数值是200 $value的值也是200;

4.3 默认参数

  1. //默认参数必须写在必选参数后面
  2. function demo3(float $a,float $b,string $opt='+')
  3. {
  4. switch($opt)
  5. {
  6. case '+':
  7. $res = "$a+$b=".($a+$b);
  8. break;
  9. case '-':
  10. $res = "$a-$b=".($a-$b);
  11. break;
  12. case '*':
  13. $res = "$a*$b=".($a*$b);
  14. break;
  15. case '/':
  16. $res = "$a/$b=".($a/$b);
  17. break;
  18. default:
  19. $res = '非法';
  20. }
  21. return $res;
  22. }
  23. echo demo3(1,2);

4.4 剩余参数

  1. function demo5()
  2. {
  3. // return func_num_args():参数数量
  4. // return func_get_arg(1);根据索引返回参数
  5. $sum = 0;
  6. for ($i = 0; $i < func_num_args(); $i++) {
  7. $sum += func_get_arg($i);
  8. }
  9. return $sum;
  10. }
  11. print_r(demo5(3,4,5,6,7));
  12. //...:参数归纳符用在定义函数参数中
  13. function demo4(...$args){
  14. //数组求和
  15. return array_sum($args);
  16. }
  17. echo demo4(1,2,3,4,5);
  18. //调用函数用...展开
  19. $arr = [1,2,3,4,5,6];
  20. echo demo4(...$arr);

总结

这节课详细的讲了函数中的三个四:函数的四种种类,四种返回值,四种参数类型。函数实现了代码共享,可以在全局调用,是十分重要的知识点。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:能将函数理解到这个层次不容易
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!