Blogger Information
Blog 16
fans 0
comment 0
visits 6126
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0810—PHP作用域和常用字符串函数
glen
Original
305 people have browsed it

函数内部使用外部变量的5种方式和常用字符串函数

1.PHP作用域
  1. 函数内部使用外部变量的5种方式演示
  2. global
  3. fn()=>(…)
  4. $GLOBALS[‘outer’]
  5. function ($outer) {…}
  6. function () use ($outer) {…}
  1. 代码:
  2. <?php
  3. namespace _0810;
  4. $name = '早上好';
  5. function hello(): string
  6. {
  7. echo '<hr>----------------------1.global关键字---------------------------<br>';
  8. global $name;
  9. return '关键字:Hello, ' . $name;
  10. }
  11. echo hello();
  12. function hello1(): string
  13. {
  14. echo '<hr>-------------------2.超全局数组: $GLOBALS[]-----------------------<br>';
  15. return '全局数组:Hello, ' . $GLOBALS['name'];
  16. }
  17. echo hello1 ();
  18. echo '<hr>-----------------3.function () use ($outer) {...}-----------------------------------</br>';
  19. $hello2 = function () use($name):string{
  20. return '匿名函数/闭包:Hello, ' . $name;
  21. };
  22. echo $hello2 ();
  23. echo '<hr>------------箭头函数: fn()=>(...)---------------<br>';
  24. $name = '大家好';
  25. $hello = fn() =>'hello,' . $name;
  26. echo $hello() ;
  27. echo '<hr>-------------------- 5. 纯函数: 将函数依赖的外部数据,通过参数注入到函数内部-------------------------<br>';
  28. /* 纯函数:将函数依赖的外部数据,通过参数注入到函数内部 */
  29. $name = '吃了吗';
  30. $hello = function($name):string
  31. {
  32. return 'hello,' .$name;
  33. };
  34. echo $hello($name);

字符串函数
  1. <?php
  2. namespace _0810;
  3. echo '<hr>----------------------md5 /* md5 对明文密码加密 */--------------------------------<br>';
  4. $pwd = 123456789;
  5. echo md5($pwd);
  6. echo '<hr>----------------------str_pad : 填充字符串的长度--------------------------------<br>';
  7. echo str_pad($pwd, 10, '*');
  8. echo '<hr>-----------------------strstr(strchr) :/* 查询字符串首次出现 */----------------------------------------------------------------</br>';
  9. $email ='name@example.com';
  10. $domain = strstr($email,'@');
  11. echo $domain;
  12. echo '<hr>-----------------------srtlen :/* 函数可以获取一个字符串的长度,直接传入字符串即可,函数返回该字符串的长度 */----------------------------------------------------------------</br>';
  13. $str ='hello world';
  14. echo strlen($str),'<br/>';
  15. $str = '我爱中国';
  16. echo strlen($str);
  17. echo '<hr>----------------------strtoupper :/* 将字符串转化为大写 */--------------------------------<br>';
  18. $b = 'hello word';
  19. echo strtoupper($b);
  20. echo '<hr>----------------------strrev :/* 反转字符串 */--------------------------------<br>';
  21. echo strrev($pwd);

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