Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
php 基础运算符有加、减、乘、除、取余、赋值运算符、字符串拼接运算符等
<?php
//赋值运算符 = :变量 = 值;
$num = 66;
$str = '666老铁';
// 加+
echo '加+:' . ($num + $num);
echo '<hr>';
//减-
echo '减-:' . ($num - 33);
echo '<hr>';
//乘*
echo '乘*:' . ($num * 2);
echo '<hr>';
//除/
echo '除/:' . ($num / 2);
echo '<hr>';
//取余%
echo '取余%:' . ($num % 4);
echo '<hr>';
//幂运算
echo '幂运算:' . ($num ** 2);
echo '<hr>';
//字符串运算符.
echo '字符串运算符:' . $str . $str;
echo '<hr>';
//如果字符串开头是数字也可以与数字相加,但是直接加会报错,可以把字符串转为数字类型
echo '字符串与数字相加:' . ($num + (int)$str);
?>
<?php
echo "echo(\"string\"):" . "string";
echo "<hr>";
echo "explode(\",\", \"string1,string2,string3\"):";
echo "<pre>";
print_r(explode(",", "string1,string2,string3"));
echo "<hr>";
echo "trim(\" string \"):";
echo trim(" string ");
echo "<hr>";
echo "ltrim(\" string \"):";
echo ltrim(" string ");
echo "<hr>";
echo "rtrim(\" string \"):";
echo rtrim(" string ");
echo "<hr>";
echo "strlen(\"string\"):";
echo strlen("string");
echo "<hr>";
echo "strtoupper(\"StRing\"):";
echo strtoupper("StRing");
echo "<hr>";
echo "strtolower(\"StRing\"):";
echo strtolower("StRing");
echo "<hr>";
echo "substr_count(\"string1,string2,string3\", \"string\"):";
echo substr_count("string1,string2,string3", "string");
echo "<hr>";
echo "substr_replace(\"string1,string2,string3\", \"str\", 8, 7):";
echo substr_replace("string1,string2,string3", "str", 8, 7);
echo "<hr>";
echo "substr(\"string1,string2,string3\", 8, 7):";
echo substr("string1,string2,string3", 8, 7);
echo "<hr>";
echo htmlspecialchars("strip_tags(<div>string</div>):");
echo strip_tags("<div>string</div>");
echo "<hr>";
echo htmlspecialchars("htmlspecialchars(\"<div>string</div>\"):");
echo htmlspecialchars("<div>string</div>");
?>
自定义函数需要以 function 关键字开头后面跟一个函数名,函数名的命名要求跟变量一致,后面跟小括号,小括号内可以加入一个或多个形参,再后面是大括号,大括号的代码块内加入封装的代码,函数代码块最后要用 return 将最后的内容返回,需要注意:代码块内 return 后面不要再加别的代码,因为 return 后面的代码将不再执行
<?php
//数据信息
$arr = [
[
'name' => 'admin01',
'age' => 18,
'gender' => '男',
'school' => "php middle school"
],
[
'name' => 'admin02',
'age' => 20,
'gender' => '女',
'school' => "js middle school"
],
[
'name' => 'admin03',
'age' => 22,
'gender' => '男',
'school' => "css middle school"
],
[
'name' => 'admin04',
'age' => 24,
'gender' => '女',
'school' => "html middle school"
],
[
'name' => 'admin05',
'age' => 26,
'gender' => '男',
'school' => "python middle school"
]
];
//表头信息
$head = [
"姓名",
"年龄",
"性别",
"所属学校"
];
function teacherInfo($info, $hd)
{
//建立table标签
$table = "<table style=\"border-collapse: collapse;background-color: lightgreen\">";
$table .= "<caption style=\"font-weight:bolder\">教师信息表</caption>";
$table .= "<thead><tr>";
foreach ($hd as $val) {
$table .= "<th style=\"border:1px solid;background-color: lightgray\">$val</th>";
}
$table .= "</tr></thead>";
$table .= "<tbody>";
foreach ($info as $per) {
// echo"<pre>";
// print_r($per);
$table .= "<tr>";
//常规写法
// $table .= "<td style=\"border:1px solid\">{$per['name']}</td>";
// $table .= "<td style=\"border:1px solid\">{$per['age']}</td>";
// $table .= "<td style=\"border:1px solid\">{$per['gender']}</td>";
// $table .= "<td style=\"border:1px solid\">{$per['school']}</td>";
//如果内部顺序确定,可以进一步遍历简化
foreach($per as $k=>$v){
$table .= "<td style=\"border:1px solid\">$v</td>";
}
$table .= "</tr>";
}
$table .= "</tbody>";
$table .= "</table>";
return $table;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>教师信息表</title>
</head>
<body>
<?php echo teacherInfo($arr, $head) ?>
</body>
</html>