Blogger Information
Blog 43
fans 4
comment 0
visits 19269
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP字符串函数/函数作用域
汇享科技
Original
338 people have browsed it

函数作用域

49393-r96vkr5wzn.png

  1. /**
  2. * PHP中支持函数作用域不支持块作用域
  3. */
  4. $name = '老张';
  5. //1.关键字global
  6. function f1 () :string{
  7. global $name;
  8. return 'Hello ' . $name;
  9. }
  10. echo f1() . '<hr>';
  11. //2.超全局变量$GLOBALS
  12. // var_dump($GLOBALS);
  13. $name = '老王';
  14. function f2() :string{
  15. return 'Hello '. $GLOBALS['name'];
  16. }
  17. echo f2() . '<hr>';
  18. //3.使用匿名函数+关键字use
  19. $name = '老刘';
  20. $f3 = function()use($name){
  21. return 'Hello '. $name;
  22. };
  23. echo $f3() . '<hr>';
  24. //4.使用箭头函数 ! 箭头函数在PHP中是鸡肋!
  25. $name = '老孙';
  26. $f4 = fn()=>'Hello ' . $name;
  27. echo $f4().'<hr>';
  28. //5.纯函数 外部注入
  29. $name = '老黄';
  30. function f5($name){
  31. return 'Hello '.$name;
  32. }
  33. echo f5($name);

字符串函数

04737-3rf37ks6w17.png

  1. $str = 'woshihuixianggege';
  2. $str1 = 'huixiang';
  3. // strripos 查找字符串在另一字符串中最后一次出现的位置(不区分大小写)
  4. echo strripos($str,$str1);
  5. echo '<hr>';
  6. //strtotime 将一个时间格式的字符串转化为时间戳
  7. $time = '2021-10-20';
  8. echo strtotime($time).'<hr>';
  9. // md5将字符串用md5格式加密
  10. $pwd = '123456';
  11. echo md5($pwd).'<hr>';
  12. // strpbrk返回指定字符第一次出现的位置开始的剩余部分。如果失败,则返回 FALSE
  13. echo strpbrk($str,'h').'<hr>';
  14. //ucwords将单词首字母转为大写
  15. $username = 'wang er ma zi';
  16. echo ucwords($username);
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!