Correcting teacher:灭绝师太
Correction status:qualified
Teacher's comments:总结的很好, 你是怎么输出可爱的小表情的呢
function 函数名(参数类型:可选,参数):返回值类型限定
{
函数体
return 返回值(返回任意类型,单个值,之后代码不会执行);
}
//无返回值
function f1()
{
return md5("abc");
}
echo f1(), "<hr>";
//返回指定类型
function f2($a, $b): int
{
return $a + $b;
}
echo f2(1.1, 2.2), "<hr>"; //3
//返回数组
function f3(): array
{
return ['name' => 'Em', 'age' => 18];
}
$arr = f3();
print_r($arr); //Array ( [name] => Em [age] => 18 )
echo '<hr>';
//返回对象 以匿名类返回
function f4(): object
{
return new class
{
public $id = '0001';
public $gender = 'male';
};
}
$user = f4();
var_dump($user);
//object(class@anonymous)#1 (2) { ["id"]=> string(4) "0001" ["gender"]=> string(4) "male" }
//访问对象属性
echo $user->gender; //male
echo '<hr>';
//返回JSON字符串
function f5(): string
{
global $arr;
return json_encode($arr);
}
$jsonStr = f5();
echo $jsonStr; // {"name":"Em","age":18}
//解析JSON字符串,还原成PHP可以处理的数据类型
$jd = json_decode($jsonStr);
var_dump($jd);
//object(stdClass)#2 (2) { ["name"]=> string(2) "Em" ["age"]=> int(18) }
//JSON字符串解析为数组
$jd = json_decode($jsonStr, true);
var_dump($jd);
//array(2) { ["name"]=> string(2) "Em" ["age"]=> int(18) }
//返回 序列化字符串
function f6()
{
global $arr;
return serialize($arr);
}
echo f6();
//a:2:{s:4:"name";s:2:"Em";s:3:"age";i:18;}
file_put_contents('f6.txt', f6());
//反序列化
var_dump(unserialize(f6()));
//array(2) { ["name"]=> string(2) "Em" ["age"]=> int(18) }
# 循环创建表格
function createTable(int $row, int $col)
{
$table = "<table border='1' cellspacing='0'>";
for ($i = 0; $i < $row; $i++) {
$table .= "<tr>";
for ($j = 0; $j < $col; $j++) {
$table .= "<td>1</td>";
}
$table .= "</tr>";
}
$table .= "</table>";
return $table;
}
echo createTable(2, 3), '<hr>';
// 参数有默认值
function createTable2(int $row = 2, int $col = 2)
{
$table = "<table border='1' cellspacing='0'>";
for ($i = 0; $i < $row; $i++) {
$table .= "<tr>";
for ($j = 0; $j < $col; $j++) {
$table .= "<td>1</td>";
}
$table .= "</tr>";
}
$table .= "</table>";
return $table;
}
echo createTable2(), '<hr>';
// 参数部分有默认值,部分没有默认值
function createTable3(int $row, int $col, string $bgcolor = "lightblue", string $text = '?')
{
$table = "<table border='1' cellspacing='0' bgcolor={$bgcolor}>";
for ($i = 0; $i < $row; $i++) {
$table .= "<tr>";
for ($j = 0; $j < $col; $j++) {
$table .= "<td>{$text}</td>";
}
$table .= "</tr>";
}
$table .= "</table>";
return $table;
}
echo createTable3(5, 6), '<hr>';
// 引用参数
function fo(&$p)
{
return $p .= $p;
}
$v1 = '?';
echo fo($v1), '<br>';
echo $v1, '<hr>';
# 剩余参数
function getSum(...$params)
{
return array_sum($params);
}
$arr = [1, 2, 3, 4, 5];
echo getSum(...$arr), '<hr>'; //15
echo getSum(1, 2, 3); //6
<?php
# 匿名函数 闭包
$closure = function ($name) {
return 'name: ' . $name;
};
echo $closure('Em'), '<br>'; // name: Em
# 闭包通过use 可以访问函数外部的自由变量/父作用域中的变量
$name = 'Em';
$age = 22;
$closure2 = function () use ($name, $age) {
return sprintf('name: %s <br> age: %d', $name, $age);
};
echo $closure2(), '<br>';
//name: Em
//age: 22
#闭包通过use引用传递
$closure3 = function ($newAge) use (&$age) {
$age = $newAge;
};
$closure3(18);
echo $age, '<br>'; //18
#闭包常用作函数返回值
function foo($animal)
{
return function ($sound) use ($animal) {
return sprintf('%s: %s', $animal, $sound);
};
}
echo foo('?')('meow~');