
批改狀態(tài):合格
老師批語:
支持函數(shù)作用域,不支持塊作用域!
1. global
2. $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'] 全局變量自動成該全局數(shù)組的成員
$name = '老劉';
function hello(): string {
return 'Hello, ' . $GLOBALS['name'];
}
echo hello() . '<hr>';
// 3.函數(shù)表達式, use (外部變量)
$name = '老石';
$hello = function () use ($name) {
return 'Hello, ' . $name;
};
echo $hello() . '<hr>';
//4.箭頭函數(shù)(php7.4+)
$name = '老趙';
$hello = fn() => 'Hello, ' . $name;
echo $hello() . '<hr>';
// 5.純函數(shù): 外部數(shù)據(jù)通過參數(shù)注入到函數(shù)中
$name = '老鄒';
$hello = function ($name) {
return 'Hello, ' . $name;
};
echo $hello($name) . '<hr>';
// 1.strtoupper(string):把一個字符串轉(zhuǎn)換為大寫;
$str = 'php.cn';
echo strtoupper($str) . '<br>';
// 2.strtolower(string):把一個字符串轉(zhuǎn)換為小寫;
$str = 'PHP.CN';
echo strtolower($str) . '<br>';
// 3.ucfirst(string);把首字母大寫;
$str = 'php.cn';
echo ucfirst($str) . '<br>';
// 4.strrev(string):翻轉(zhuǎn)字符串(倒敘輸出);
$str = 'php.cn';
echo strrev($str) . '<br>';
// 5.strlen(string):返回字符串的長度;
$str = 'php.cn';
echo strlen($str) . '<br>';
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號