Blogger Information
Blog 20
fans 0
comment 0
visits 7924
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP作用域及字符串函数
P粉191340380
Original
349 people have browsed it

函数中引用外部变量的5种方法

  1. // 1. global 关键字
  2. $name = '老朱';
  3. function name(){
  4. global $name ;
  5. return $name;
  6. }
  7. echo name() . '<br>';
  8. // 2. 超全局数组: $GLOBALS['outer']
  9. $name = '老李';
  10. function hello(): string{
  11. return 'Hello, ' . $GLOBALS['name'];
  12. }
  13. echo Hello() . '<hr>';
  14. // 3. 函数表达式: function () use ($outer){...}
  15. $name = '老陈';
  16. function () use ($name) :string{
  17. return 'Hello, ' . $name;
  18. };
  19. echo hello() . '<hr>';
  20. // 4. 箭头函数: fn()=>(...)
  21. $name = '老孙';
  22. $hello = fn() => 'Hello, ' . $name;
  23. echo hello(), '<hr>';
  24. // 5. 纯函数: function($outer){...}
  25. $name = '老林';
  26. $hello = function($name){
  27. return 'Hello, ' . $name;
  28. };
  29. echo hello($name), '<hr>';

字符串函数

  1. // ucfirst:将首个字幕转换成大写
  2. $str = 'hello, world';
  3. echo ucfirst($str);
  4. echo '<hr>';
  5. // ucwords: 将字符串仲每个单词的首字符转换为大写
  6. $str = 'hello, world';
  7. echo ucwords($str);
  8. echo '<hr>';
  9. // lcfirst: 把字符串中首字符转化为小写
  10. $str = 'Hello, world';
  11. echo lcfirst($str);
  12. echo '<hr>';
  13. // strtoupper: 把字符串转换为大写
  14. $str = 'hello, world';
  15. echo strtoupper($str);
  16. echo '<hr>';
  17. // strtolower: 把字符串转换位小写
  18. $str = 'HELLO, WORLD';
  19. echo strtolower($str);
  20. echo '<hr>';

函数中引用外部变量的5种方法

  1. // 1. global 关键字
  2. $name = '老朱';
  3. function name(){
  4. global $name ;
  5. return $name;
  6. }
  7. echo name() . '<br>';
  8. // 2. 超全局数组: $GLOBALS['outer']
  9. $name = '老李';
  10. function hello(): string{
  11. return 'Hello, ' . $GLOBALS['name'];
  12. }
  13. echo Hello() . '<hr>';
  14. // 3. 函数表达式: function () use ($outer){...}
  15. $name = '老陈';
  16. function () use ($name) :string{
  17. return 'Hello, ' . $name;
  18. };
  19. echo hello() . '<hr>';
  20. // 4. 箭头函数: fn()=>(...)
  21. $name = '老孙';
  22. $hello = fn() => 'Hello, ' . $name;
  23. echo hello(), '<hr>';
  24. // 5. 纯函数: function($outer){...}
  25. $name = '老林';
  26. $hello = function($name){
  27. return 'Hello, ' . $name;
  28. };
  29. echo hello($name), '<hr>';

字符串函数

  1. // ucfirst:将首个字幕转换成大写
  2. $str = 'hello, world';
  3. echo ucfirst($str);
  4. echo '<hr>';
  5. // ucwords: 将字符串仲每个单词的首字符转换为大写
  6. $str = 'hello, world';
  7. echo ucwords($str);
  8. echo '<hr>';
  9. // lcfirst: 把字符串中首字符转化为小写
  10. $str = 'Hello, world';
  11. echo lcfirst($str);
  12. echo '<hr>';
  13. // strtoupper: 把字符串转换为大写
  14. $str = 'hello, world';
  15. echo strtoupper($str);
  16. echo '<hr>';
  17. // strtolower: 把字符串转换位小写
  18. $str = 'HELLO, WORLD';
  19. echo strtolower($str);
  20. echo '<hr>';
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!