Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
函数内部使用外部变量的5种方式
代码 :
<?php
namespace _0810;
$name = '早晨';
function hello(): string
{
echo '----------------------1.global关键字---------------------------<br>';
global $name;
return '关键字:Hello, ' . $name;
}
echo hello();
function hello1(): string
{
echo '<br>-------------------2.超全局数组: $GLOBALS[]-----------------------<br>';
return '全局数组:Hello, ' . $GLOBALS['name'];
}
echo hello1();
echo '<br>-----------------3.function () use ($outer) {...}--------------------<br>';
$hello2 = function () use ($name): string {
return '匿名函数/闭包:Hello, ' . $name;
};
echo $hello2();
echo '<br>--------------------4. 箭头函数: fn()=>(...)-------------------------<br>';
$hello3 = fn () => '箭头函数:Hello, ' . $name;
echo $hello3();
echo '<br>-------------------- 5. 纯函数-------------------------<br>';
$hello4 = function ($name): string {
return '纯函数:Hello, ' . $name;
};
echo $hello4($name);
运行效果:
<?php
namespace _0810;
// md5 对明文密码加密
echo '----------------------字符串:md5--------------------------------<br>';
$pwd = 123456;
echo md5($pwd);
echo '<br>----------------------字符串:strtolower--------------------------------<br>';
$a = 'HELLO WORD';
echo strtolower($a);
echo '<br>----------------------字符串:strtoupper--------------------------------<br>';
$b = 'hello word';
echo strtoupper($b);
echo '<br>----------------------字符串:str_split--------------------------------<br>';
print_r(str_split(md5($pwd), 3));
echo '<br>----------------------字符串:substr--------------------------------<br>';
echo substr($pwd, 2);
echo '<br>----------------------字符串:strrev--------------------------------<br>';
echo strrev($pwd);
echo '<br>----------------------字符串:str_pad--------------------------------<br>';
echo str_pad($pwd, 10, '*');
运行效果: