批改状态:合格
老师批语:
支持函数作用域,不支持块作用域!
1. global2. $GLOBALS['outer']3. function () use ($outer) {...}4. fn()=>(...)5. function ($outer) {...}
// 1.global 声明$name = '老朱';function hello(): string {global $name;return 'Hello, ' . $name;}echo hello() . '<hr>';// 2. $GLOBALS['name'] 全局变量自动成该全局数组的成员$name = '老刘';function hello(): string {return 'Hello, ' . $GLOBALS['name'];}echo hello() . '<hr>';// 3.函数表达式, use (外部变量)$name = '老石';$hello = function () use ($name) {return 'Hello, ' . $name;};echo $hello() . '<hr>';//4.箭头函数(php7.4+)$name = '老赵';$hello = fn() => 'Hello, ' . $name;echo $hello() . '<hr>';// 5.纯函数: 外部数据通过参数注入到函数中$name = '老邹';$hello = function ($name) {return 'Hello, ' . $name;};echo $hello($name) . '<hr>';
// 1.strtoupper(string):把一个字符串转换为大写;$str = 'php.cn';echo strtoupper($str) . '<br>';// 2.strtolower(string):把一个字符串转换为小写;$str = 'PHP.CN';echo strtolower($str) . '<br>';// 3.ucfirst(string);把首字母大写;$str = 'php.cn';echo ucfirst($str) . '<br>';// 4.strrev(string):翻转字符串(倒叙输出);$str = 'php.cn';echo strrev($str) . '<br>';// 5.strlen(string):返回字符串的长度;$str = 'php.cn';echo strlen($str) . '<br>';
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号