Blogger Information
Blog 34
fans 0
comment 0
visits 20282
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php之灭绝秘授函数,变量,师太亲传作用域,返回值
小庄
Original
383 people have browsed it

php之灭绝秘授函数,变量,师太亲传作用域,返回值

  1. <?php
  2. /**
  3. *
  4. * 1. 总结函数的返回值,参数?
  5. * 答:函数的参数用于给函数传入数据,函数计算后将值返回,外部可以用变量接收便于再次计算,也可以直接输出。
  6. *
  7. * 2. 实例演绎你对课上匿名函数以及变量作用域问题的理解?
  8. * 答: 1)函数是全局的,函数体中的变量只在函数体中有效。
  9. * 2)函数体外的全局变量只能在函数体外使用,函数中若要使用可以使用$GLOBALS['全局变量名']或$global $全局变量名
  10. *
  11. */
  12. $str1 = 10086;//全局变量
  13. function demo(int $num1){
  14. $demoStr1 = 100;//函数体局部变量,仅在函数体中有效
  15. echo "这是函数体中变量的值:".$demoStr1.'<br />';
  16. echo "这是函数体外的全局变量:".$GLOBALS['str1']."<br />";
  17. return function(int $num2){//将匿名函数作为返回值,匿名函数在此的作用仅仅是将计算的结果作为返回值
  18. return $num1 + $num2;
  19. };
  20. }
  21. echo "这是demo函数传递的参数与匿名函数传递的参数计算的值:".demo(12)(13);// demo(12)给demo传入实参,(13)给匿名函数传入实参
  22. // 两种调用是一样的,只是方式不同
  23. $demoTest = demo(22);//demo(22)调用并传入实参22
  24. echo "这是demo函数传递的参数与匿名函数传递的参数计算的值(效果一样):".$demoTest(12);//demoTest 是个变量但接收了demo函数的返回结果,返回结果是带有参数的匿名函数因此传入实参12
Correcting teacher:PHPzPHPz

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!