Correction status:qualified
Teacher's comments:
一、PHP中的数据类型
1. 标量类型:整型integer,浮点型float,字符型string,布尔类型boolean(特点是:值是单一的一个值,一个变量只能存一个数据)
2. 复合类型:数组array,对象object(特点是:多值变量,一个变量可以存放多个数据)
3.特殊类型:null,资源resource
二、用foreach循环遍历二维数组
<?php $title = '学生成绩管理系统'; $tableTitle = '学生成绩表'; $students = [ ['id' => 1, 'name' => '小李', 'sex' => 1, 'subject' => '数学', 'score' => 90], ['id' => 2, 'name' => '小红', 'sex' => 0, 'subject' => '数学', 'score' => 85], ['id' => 3, 'name' => '小张', 'sex' => 1, 'subject' => '数学', 'score' => 95], ['id' => 4, 'name' => '小王', 'sex' => 1, 'subject' => '语文', 'score' => 92] ]; $totel = 4; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $title ?></title> <style> table,th,td { border: 1px solid #ccc; padding: 8px; } table { border-collapse: collapse; width: 80%; text-align: center; margin: 30px auto; } thead tr:first-of-type { background-color: lightblue; } tbody tr:hover { background-color: #efefef; } table > caption { font-size: 1.2rem; margin-bottom: 15px; } table + p { text-align: center; } </style> </head> <body> <table> <caption><?php echo $tableTitle ?></caption> <thead> <tr> <th>学号</th> <th>姓名</th> <th>性别</th> <th>科目</th> <th>成绩</th> </tr> </thead> <tbody> <!-- <?php /* //方法一: $data = ''; //定义一个空值变量,用于存放数据 foreach ($students as $student) { //用foreach()函数遍历二维数组获得每一个学生的成绩信息用$student这个临时变量存储 // print_r($student); $data .= '<tr>'; $data .= '<td>' . $student['id'] . '</td>>'; $data .= '<td>' . $student['name'] . '</td>>'; $data .= '<td>' . $student['sex'] . '</td>>'; $data .= '<td>' . $student['subject'] . '</td>>'; $data .= '<td>' . $student['score'] . '</td>>'; $data .= '</tr>'; } echo $data; */?> --> <!--方法二:foreach()的替代写法 if 语句的两种写法: 1.普通 if(){} else {}写法 2.用三元运算法代替if(){}else{} --> <?php foreach($students as $student) : ?> <tr> <td><?=$student['id']?></td> <td><?=$student['name']?></td> <!-- 1.普通if() else 写法 <td> <?php /*if($student['sex'] === 1) : */?> 男 <?php /*else : */?> 女 <?php /*endif; */?> </td>--> <!-- 2.三元运算符写法--> <td><?=$student['sex'] ? '男' : '女' ?></td> <td><?=$student['subject']?></td> <td><?=$student['score']?></td> </tr> <?php endforeach; ?> </tbody> </table> <p>总计<?php echo $totel ?>人</p> </body> </html>
点击 "运行实例" 按钮查看在线实例