Blogger Information
Blog 28
fans 0
comment 0
visits 15726
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
08-04作业 : 函数的参数,闭包 ,全局作用域
︷肉園耔︷
Original
541 people have browsed it

— 函数的参数,闭包 ,全局作用域

  1. <?php
  2. /**函数:完成特定功能的代码块 */
  3. echo strtoupper('imzchloe');
  4. echo strlen ('wwwppcns');
  5. echo abs(-45);
  6. /*
  7. function 函数名称([参数列表 -参数)
  8. {
  9. 函数体
  10. return 返回值
  11. 函数只能返回单个值 return 后面的代码不会执行
  12. }*/
  13. ob_clean();
  14. function divide($one,$two){
  15. return $one/$two;
  16. }
  17. echo divide(99,9);
  18. function demo(){
  19. return 'nihao';
  20. return true;
  21. }
  22. var_dump(demo());
  23. ob_clean();
  24. function test()
  25. {
  26. return['status'=>1,'msg'=>'登录成功'];
  27. }
  28. print_r(test());
  29. //多个值以对象的形式返回
  30. function demo2(){
  31. return new class(){
  32. public $name='chloe';
  33. public $gender='女';
  34. };
  35. }
  36. $obj=demo2();
  37. var_dump($obj);
  38. echo $obj->name;
  39. echo $obj->gender;
  40. function demo3(){
  41. //json_encode编码转换json字符串
  42. return json_encode(['status'=>1,'msg'=>'验证成功']);
  43. }
  44. var_dump(demo3());
  45. $res=demo3();
  46. var_dump(json_decode($res,true) );
  47. <?php
  48. /**命名函数是全局成员 不受作用域限制
  49. **匿名函数是闭包
  50. **/
  51. /*function getName($name){
  52. return "欢迎{$name}来到php中文网做客";
  53. }
  54. echo getName('高洛峰先生');
  55. $closure = function ($name){
  56. return "欢迎{$name}来到php中文网做客<br>";
  57. };
  58. echo $closure ('胡歌');
  59. //变量的作用域
  60. //全局变量 函数体外声明的变量
  61. $itemName='iphone 12 por max 256g';
  62. $itemPice=12342;
  63. function getItemInfo(){
  64. return sprintf('今日主推是的%s,商品价格是%d',
  65. $itemName,$itemPrice);
  66. }*/
  67. //返回 一个3行2列的表格
  68. function createTable(int $rows, int $cols,string $content, $bgColor):string
  69. {
  70. $table = "<table border='1' cellspacing='0' cellpadding='0' bgColor='$bgColor'>";
  71. for ($i = 0; $i < $rows; $i++) {
  72. $table .= "<tr>";
  73. for ($j = 0; $j < $cols; $j++) {
  74. $table .= "<td>$content</td>";
  75. }
  76. $table .= "</tr>";
  77. }
  78. $table .= '</tbale>';
  79. return $table;
  80. }
  81. //echo createTable(3,6,'红','red');
  82. //参数:实参
  83. function createTable1(int $rows =7,int $cols=6,string $content='大',string $bgColor='green'):string{
  84. $table = "<table border='1' cellspacing='0' cellpadding='0' bgColor='$bgColor'>";
  85. for ($i = 0; $i < $rows; $i++) {
  86. $table .= "<tr>";
  87. for ($j = 0; $j < $cols; $j++) {
  88. $table .= "<td>$content</td>";
  89. }
  90. $table .= "</tr>";
  91. }
  92. $table .= '</tbale>';
  93. return $table;
  94. }
  95. //echo createTable1('留','orange');
  96. /**
  97. 1.参数列表是从左到右
  98. 2.不传参数或者少传参数,都会使用默认的参数值,如果用户传了参数,会发生覆盖
  99. *
  100. */
  101. //echo createTable1(8,8);
  102. echo createTable1();
  103. echo createTable1(5,6,'pink','pink');
  104. //剩余参数 适应于参数个数不确定
  105. if(!function_exists('test'))
  106. {
  107. //当剩余参数用于参数列表中 收集
  108. function test($a,$b,$c,$d,$e){
  109. return $a+$b+$c+$d+$e;
  110. }
  111. echo test(1,2,3,4,5);
  112. }
  113. ob_clean();
  114. //剩余参数 适应于 参数的个数不确定
  115. function test1(...$args){ //剩余参数用于参数列表中表示收集作业
  116. var_dump($args);
  117. return array_sum($args);
  118. }
  119. echo test1(222123,5434,132154,44,6871);
  120. $arr=[123,234,523,543];
  121. echo test1(...$arr); //剩余参数用在函数的调用表达式中:展开
  122. //数据库连接
  123. function connect(...$arg){
  124. return new pdo1($arg[0],$arg[1],$arg[2]);
  125. }
  126. $pdo=connect('mysql:dbname=news','root','zhoujielun521');
  127. var_dump($pdo);
  128. if($pdo)echo "数据库连接成功";
  129. <?php
  130. /*
  131. * 命名函数是全局成员 不受作用域限制
  132. * */
  133. function getName($name){
  134. return "欢迎{$name}来到php中文做客";
  135. }
  136. echo getName('高洛峰先生');
  137. //变量的作用域
  138. //全局变量 函数体外声明的变量
  139. $itemName ='iphone 12 por max 256g';
  140. $itemPrice =12585;
  141. /*function getItemInfo(){
  142. return sprinft('今日主推产品$s,商品价格是%d',$itemName,$itemPrice);
  143. }
  144. echo getItemInfo();*/
  145. function getItemInfo_pro(){
  146. //global $itemPrice,$itemName;
  147. return sprintf('今日主推产品是%s,商品价格是%d<br>',$GLOBALS['itemName'],$GLOBALS['itemPrice']);
  148. }
  149. echo getItemInfo_pro();
  150. //局部变量 在函数体内声明的变量
  151. function add(){
  152. //子作用域
  153. $leftNum=25;
  154. $rightNum=56;
  155. echo "$leftNum + $rightNum =".($leftNum+ $rightNum);
  156. }
  157. add();
  158. //echo $leftNum;
  159. ob_clean();
  160. //闭包
  161. $clouser1=function() use ($itemName,$itemPrice)
  162. {
  163. return sprintf('今日主推产品是%s,商品价格是%d<br>',$GLOBALS['itemName'],$GLOBALS['itemPrice']);
  164. };
  165. echo $clouser1();
  166. ob_clean();
  167. //闭包作为函数的返回值
  168. function getSite($site){
  169. return function ($color) use ($site){
  170. return sprintf('<h1 style="color:%s"><center>%s</center></h1>',$color,$site);
  171. };
  172. }
  173. //函数的返回值是b闭包
  174. $closure2= getSite('淘宝网');
  175. echo $closure2('orange');
  176. echo getSite('拼多多')('blue');
  177. //闭包可以改变变量上下文的值
  178. $name='九阴白骨爪';
  179. $closure3 = function($newName) use($name){
  180. $name=$newName;
  181. };
  182. $closure3('一阳指');
  183. echo $name;
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