Blogger Information
Blog 24
fans 0
comment 12
visits 15564
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
细说函数作业与感想
移动用户-5435854
Original
586 people have browsed it

4 月 20 日作业:

  1. 举例演示函数的四种类型
  2. 举例演示函数多值返回的四种形式
  3. 举例演示函数的四种参数

1、举例演示函数的四种类型

1.自定义函数:用户自己定义的

  1. /小驼峰命名法
  2. function userName()
  3. /大驼峰命名法
  4. function UserName()
  5. /蛇形命名法 通常命名公共函数
  6. function user_name()

2.系统函数

  1. $str = '举例演示函数的四种类型';
  2. echo mb_substr($str,0,5);

3.可变函数 (把自定义函数套入一个变量里面)

  1. function sales(float $pay,float $discount):float
  2. {
  3. return $pay*$discount;
  4. }
  5. $paysales='sales';
  6. echo $paysales(1000,0.9);

4.匿名函数 (个人感觉就是可变函数改一种写法而已)

  1. $paysales=function (float $pay,float $discount):float
  2. {
  3. return $pay*$discount;
  4. };
  5. echo $paysales(1000,0.9);
  1. $f = function ($discount) {
  2. return function (float $money, int $num) use ($discount) : float
  3. {
  4. $amount = $money * $num;
  5. return $amount >= 2000 ? $amount * $discount : $amount;
  6. };
  7. };
  8. echo $f(0.6)(5000,2);

2. 举例演示函数多值返回的四种形式

1.字符串拼装

  1. function ceshi():string
  2. {
  3. $denglu=1;
  4. $chenggong='成功';
  5. return $denglu . $chenggong;
  6. }
  7. echo ceshi();

2.通过数组返回

  1. function ceshi2():array
  2. {
  3. return ['denglu'=>1,'chenggong'=>'成功'];
  4. }
  5. echo '<pre>'.print_r(ceshi2(),true) .'</pre>' ;
  1. function ceshi2():array
  2. {
  3. return ['denglu'=>1,'chenggong'=>'成功'];
  4. }
  5. // echo '<pre>'.print_r(ceshi2(),true) .'</pre>' ;
  6. echo ceshi2()['denglu']==1?'成功':'登录失败';

3.通过 json 返回

  1. function ceshi3():string{
  2. return json_encode(['denglu'=>1,'chenggong'=>'成功']);
  3. }
  4. echo ceshi3();
  5. $var=json_decode($ceshi);
  6. echo '<hr>';
  7. var_dump($var );

4.序列化返回值

  1. echo ceshi4();
  2. $ceshiyixia=unserialize(ceshi4());
  3. echo '<hr>';
  4. echo $ceshiyixia;

3. 举例演示函数的四种参数

1.值参数

  1. function aaa(float $id):float
  2. {
  3. return $id+=5;
  4. }
  5. $bianliang=100;
  6. echo aaa($bianliang);
  7. echo $bianliang;

2.引用传递

  1. function bbb(float &$id):float
  2. {
  3. return $id+=5;
  4. }
  5. $bianliang=100;
  6. echo bbb($bianliang);
  7. echo '<br>';
  8. echo $bianliang;

3.默认参数

  1. echo '<hr>';
  2. function ccc(float $id=100):float
  3. {
  4. return $id+=5;
  5. }
  6. echo ccc();
  7. echo '<br>';

4.剩余参数

  1. function ddd(...$qiuhe):float
  2. {
  3. return array_sum($qiuhe);
  4. }
  5. print_r(ddd(6,9,34,10));

感想

今天讲的是函数的四种类型,函数的四种返回值和函数的四种参数。挺绕的,朱老师教的过程中,一方面要照顾到我这种小白学员,零基础讲,又要照顾到本身从事php开发工作的学员,讲一些晋级的知识,对于我们小白学员来说,如果全都掌握,我认为还是有一定的难度的,所以,大脑先屏蔽掉一些特别深奥的内容(比如闭包)先把基础应该掌握的都掌握是王道。慢慢来。

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
2 comments
移动用户-5435854 2020-04-21 20:02:05
辛苦了辛苦了
2 floor
移动用户-5435854 2020-04-21 20:01:14
哈哈哈哈哈
1 floor
Author's latest blog post