Blogger Information
Blog 36
fans 1
comment 0
visits 26094
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP作用域与字符串常用函数
早晨
Original
459 people have browsed it

PHP作用域

函数内部使用外部变量的5种方式

  1. global
  2. $GLOBALS[‘outer’]
  3. function () use ($outer) {…}
  4. fn()=>(…)
  5. function ($outer) {…}

代码 :

  1. <?php
  2. namespace _0810;
  3. $name = '早晨';
  4. function hello(): string
  5. {
  6. echo '----------------------1.global关键字---------------------------<br>';
  7. global $name;
  8. return '关键字:Hello, ' . $name;
  9. }
  10. echo hello();
  11. function hello1(): string
  12. {
  13. echo '<br>-------------------2.超全局数组: $GLOBALS[]-----------------------<br>';
  14. return '全局数组:Hello, ' . $GLOBALS['name'];
  15. }
  16. echo hello1();
  17. echo '<br>-----------------3.function () use ($outer) {...}--------------------<br>';
  18. $hello2 = function () use ($name): string {
  19. return '匿名函数/闭包:Hello, ' . $name;
  20. };
  21. echo $hello2();
  22. echo '<br>--------------------4. 箭头函数: fn()=>(...)-------------------------<br>';
  23. $hello3 = fn () => '箭头函数:Hello, ' . $name;
  24. echo $hello3();
  25. echo '<br>-------------------- 5. 纯函数-------------------------<br>';
  26. $hello4 = function ($name): string {
  27. return '纯函数:Hello, ' . $name;
  28. };
  29. echo $hello4($name);

运行效果:

字符串代码

  1. <?php
  2. namespace _0810;
  3. // md5 对明文密码加密
  4. echo '----------------------字符串:md5--------------------------------<br>';
  5. $pwd = 123456;
  6. echo md5($pwd);
  7. echo '<br>----------------------字符串:strtolower--------------------------------<br>';
  8. $a = 'HELLO WORD';
  9. echo strtolower($a);
  10. echo '<br>----------------------字符串:strtoupper--------------------------------<br>';
  11. $b = 'hello word';
  12. echo strtoupper($b);
  13. echo '<br>----------------------字符串:str_split--------------------------------<br>';
  14. print_r(str_split(md5($pwd), 3));
  15. echo '<br>----------------------字符串:substr--------------------------------<br>';
  16. echo substr($pwd, 2);
  17. echo '<br>----------------------字符串:strrev--------------------------------<br>';
  18. echo strrev($pwd);
  19. echo '<br>----------------------字符串:str_pad--------------------------------<br>';
  20. echo str_pad($pwd, 10, '*');

运行效果:

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!