批改状态:合格
老师批语:多使用自己例子
<?php// 变量命名与 js规则一样,驼峰 蛇形namespace _0809;// 变量赋值// 1、默认:值传递$username = '小狗';printf('$uername = %s <br>',$username);// 更新$myName = $username;printf('$uername = %s | $myName = %s <br>',$username,$myName );// 2、引用传递// 变量前 :& 取地址符 表示引用$yourName = &$username;$yourName = '小猫';printf('$uername = %s | $yourName = %s <br>',$username,$yourName );// 引用传递, 会同步更新原始变量,因为你引用是同一个变量// "其实引用传递,并没有创建新变量", 而是给原变量,起了一个别名// 例如,提到川建国, 大家都知道指的是美丽国前总统特朗普,当然,懂王也是它// 所以, 可以给同一个变量, 起多个别名,是完全可以的, 大家可以试试看
<?php// 数据类型// 1、基本类型 :不可再分// 1.1数值 int floatuse Demo1 as GlobalDemo1;use Demo2 as GlobalDemo2;$age = 20;$price =80.08;// 1.2 字符串 string$username ='小牛';// 1.3 布尔 boolean$isDel = true;// 2、复合类型// 数组$arr = [20,'root',true];// 对象// 类// class Demo{}// $obj = new Demo();// 匿名类$obj = new class ( 1000){public float $salary;public function __construct(float $salary){$this -> salary = $salary;}};echo gettype($obj);echo $obj->salary .'<br>';// 3、null// 三种情况为null// 1. 赋值 null, 2. 没赋值 3. 被销毁 unset()// $a;// echo gettype($a);// $b = null;// echo gettype($b);// $c = 12;// unset ($c);// echo gettype($c);// 4、 资源类型// fopen() 打开文件 r 只读 rw 读写 + 追加$f = fopen('zuoye.php','r');echo gettype($f) . '<br>';// callback: 回调// php用字符串来传递函数,所以可以以任何形式传递函数// 所以回调函数不仅包括函数 ,还包括对象方法,静态类方法等// 接受回调的函数很多,例如前面遇到的array_filterfunction hello (string $name ,$salary) :string {return '你好,' . $name .',工资是'.$salary;}echo hello('小龙','123') .'<br>';// echo call_user_func( 函数,参数列表)echo call_user_func('hello' ,'小蛇',456).'<br>';$params = ['小鸡',1122];echo call_user_func_array('hello' ,$params).'<br>';// 推荐使用 call_user_func_array/*** call_user_func(函数名, 参数列表)* call_user_func_array(函数名, [参数列表...])* 哪怕只有一个参数,也尽量使用第二种** call_user_func_array()的第一个参数是要执行的函数* 这个函数参数可以有三个来源* 1. 用户自定义的函数* 2. 对象方法* 3. 类方法/静态方法*/// 如果这个函数来自对象方法// 创建Demo1类class Demo1 {public function hello ( string $name) : string{return '你好,' . $name;}}// new 一个实例$obj =new Demo1;// hello 不是全局函数 是一个对象方法 固定语法 写到数组echo call_user_func_array([$obj,'hello'],['小猴']).'<br>';// 如果这个函数来自: 类方法 static 静态化// 创建Demo2类class Demo2 {public static function hello ( string $name) : string{return '你好,' . $name;}}// 类方法是用类调用,不需要对象调用echo call_user_func_array(['Demo2','hello'],['小猪']).'<br>';
<?phpnamespace _0809;// 分支 流程控制// 单分支 if$age = 19;if ($age >=18){echo '已经成年 <br>';}// 双分支$age = 15;if ($age >=18){echo '已经成年 <br>';} else {echo '未成年 <br>';}// 双分支语法糖 三元运算符// 条件? true :false$age =19;echo $age >18 ?'已经成年<br>':'未成年<br>';// 多分支、$age =-1;if ($age >=18 && $age<30){echo "{$age}岁了,该结婚了<br>";} elseif ($age >=30 && $age <100){echo "{$age}岁了<br>";}elseif ($age >=100||$age <0){echo "非法年龄<br>";}else {echo"{$age}岁了,未成年<br>";}// $age = 120;// if ($age >= 18 && $age < 30) {// echo "{$age}岁,彩礼,能按揭吗?<br>";// } elseif ($age >= 30 && $age < 45) {// echo "{$age}岁,应该成个家了<br>";// } elseif ($age >= 45 && $age < 100) {// echo "{$age}岁,房贷快还完了<br>";// } elseif ($age >= 100 || $age <= 6) {// echo "{$age}是一个非法年龄<br>";// } else {// // 默认分支// echo "{$age}岁,放学,我送你回家<br>";// }// 多分支 语法糖 switch$age = 50 ;switch (true) {case $age >18 && $age <60 :echo "{$age}岁了,不能退休<br>";break;case $age >60 :echo "{$age}岁了,退休了<br>";break;default:echo "非法年龄<br>";}/*** 分支* 1. 单分支* 2. 双分支: 三元* 3. 多分支: switch*/
<?phpnamespace _0809;// 循环$colors = ['red','green','blue'];$i =0;// count() 计算数组长度echo '数组长度:',count($colors),'<br>';$list = '<ul style = "border:2px solid">';if ($i < count($colors)){$list.="<li>{$colors[$i]}</li>";}$i++;// $i =$i + 1;// $i+=1;if ($i < count($colors)) {$list .= "<li>{$colors[$i]}</li>";}$i++;if ($i < count($colors)) {$list .= "<li>{$colors[$i]}</li>";}// $list = $list.'</ul>';// 简化$list .= '</ul>';echo $list;/*** 循环三要素* 1. $i = 0,初始化* 2. $i < count($colors), 循环条件* 3. $i++, 更新循环条件, 否则进入死循环*/$list = '<ul style = "border:2px solid blue">';// 1. $i = 0,初始化$a =0 ;// 2. $i < count($colors), 循环条件// 入口判断while ($a < count($colors)){$list .= "<li>{$colors[$a]}</li>";// $i++, 更新循环条件$a++;};$list .= '</ul>';echo $list;$list = '<ul style = "border:2px solid green">';// 1. $i = 0,初始化$b=0;// 2. $i < count($colors), 循环条件do {$list .= "<li>{$colors[$b]}</li>";// 3. $b++, 更新循环条件$b=$b + 1;} while ($b < count($colors));$list .= '</ul>';echo $list;$list = '<ul style = "border:2px solid red">';for ($c = 0;$c <count($colors);$c++){$list .= "<li>{$colors[$c]}</li>";}$list .= '</ul>';echo $list;$list = '<ul style = "border:2px solid red">';for ($c = 0;$c <count($colors);$c++){if($c>1) {// break 跳出循环 。break;}$list .= "<li>{$colors[$c]}</li>";}$list .= '</ul>';echo $list;$list = '<ul style = "border:2px solid blue">';for ($d = 0;$d<count($colors);$d++){if($d === 1) {// 跳过某个循环 。continue; }$list .= "<li>{$colors[$d]}</li>";}$list .= '</ul>';echo $list;
<?phpnamespace _0809;// 数组遍历$colors = ['red','green','blue'];print_r($colors);// <pre > 格式化打印printf('<pre>%s</pre>',$colors);echo'<hr>';printf('<pre>%s</pre>',print_r($colors));echo'<hr>';printf('<pre>%s</pre>',print_r($colors,true));echo'<hr>';// 1、索引数组:键名默认从 0开始的 递增的正整数(数值类型)// 完整的声明语法$colors = ['0'=>'red','1'=>'blue','2'=>'green'];// 用默认的键名可以不写$colors = ['red','green','blue'];printf('<pre>%s</pre>',print_r($colors,true));echo $colors[1];echo '$colors[1]';echo '<hr>';// 2、关联数组 键名是字符串(语义化)$user = [123,'小王',99];$user = ['id'=>123,'name'=>'小王','score'=>99];print $user;printf('<pre>%s</pre>',print_r($user,true));// printf('<pre>%s</pre>',print_r($user));// printf('<pre>%s</pre>',print_r($user[1]));// printf('<pre>%s</pre>', print_r($user, true));echo $user['name']. '<br>';// $arr[$key]// $key: 数值->索引数组// $key: 字符串->关联数组// 数组遍历 :while循环 for循环// 数组的专用遍历语法 :foreach($arr as $key=>$value){...}foreach($colors as $value){echo $value.'<br>';}foreach($colors as $key =>$value){echo $value.'<br>';printf('[%s]=>%s<br>',$key,$value);}echo '<hr>';foreach($user as $key =>$value){// echo $value.'<br>';printf('[%s]=>%s<br>',$key,$value);}// 二维数组// 因为从数据表中的查询结果,php用二维数组来表示// gender : 0男 1女$users =[0=>['id'=>123,'name'=>'小王','score'=>99,'gender'=>0],1=>['id'=>456,'name'=>'小猫','score'=>99,'gender'=>1],2=>['id'=>789,'name'=>'小狗','score'=>99,'gender'=>0],];printf('<pre>%s</pre>',print_r($users,true));// foreach +table 放到渲染到页面上$table = '<table border="1" width="400" cellspacing="0" cellpadding="3" align="center">';$table .= '<caption>用户信息表</caption>';$table .= '<thead bgcolor="#ccc"><tr><th>ID</th><th>用户名</th><th>性别</th><th>成绩</th></tr></thead>';$table .= '<tbody align="center">';// 遍历foreach($users as $user ){print_r($user);$table .='<tr>';$table .="<td>{$user['id']}</td>";// 简化、 拼接// $table .='<td>'.$user['id'].'</td>';$table .="<td>{$user['name']}</td>";$table .='<td>'.($user['gender']?'女':'男').'</td>';$table .='<td>'.$user['score'].'</td>';$table .='</tr>';}$table .= '</table>';// 打印表格到页面中echo $table;
<!-- 数据库查询利用二维数组模拟数据查询结果--><?php$stus = [['id' => 1, 'name' => '刘备', 'course' => 'js', 'score' => 83],['id' => 2, 'name' => '关羽', 'course' => 'php', 'score' => 75],['id' => 3, 'name' => '张飞', 'course' => 'js', 'score' => 52],['id' => 4, 'name' => '孙权', 'course' => 'php', 'score' => 88],['id' => 5, 'name' => '周瑜', 'course' => 'js', 'score' => 65],['id' => 6, 'name' => '孔明', 'course' => 'php', 'score' => 53],['id' => 7, 'name' => '赵云', 'course' => 'js', 'score' => 63],['id' => 8, 'name' => '马超', 'course' => 'js', 'score' => 77],['id' => 9, 'name' => '姜维', 'course' => 'php', 'score' => 93],['id' => 10, 'name' => '黄忠', 'course' => 'js', 'score' => 41],]?><!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><style>table {border-collapse: collapse;width: 360px;text-align: center;}table th,table td {border: 1px solid #000;padding: 5px;}table caption {font-size: 1.3em;}table thead {background-color: lightcyan;}.active {color: red;}</style></head><body><table><caption>学生成绩表</caption><thead><tr><th>ID</th><th>姓名</th><th>课程</th><th>成绩</th></tr></thead><tbody><?phpforeach($stus as $stu ){echo '<tr>';echo "<td>{$stu['id']}</td>";// 简化、 拼接// $table .='<td>'.$user['id'].'</td>';echo "<td>{$stu['course']}</td>";echo "<td>{$stu['name']}</td>";echo '<td>'.$stu['score'].'</td>';echo '</tr>';}?></tbody><!-- <hr> --><tbody><?phpforeach($stus as $stu ){?><tr><td><?php echo $stu['id'] ?></td><td><?php echo $stu['course'] ?></td><td><?php echo $stu['name'] ?></td><td><?php echo $stu['score'] ?></td></tr><?php }?></tbody><tbody><?php// 将{ 左侧大括号 用: 代替 右侧的大括号用 foreach代替 } 模板语法// 短标签 将 ?php 用 = 代替 叫短标签foreach($stus as $stu ) : ?><tr><td><?= $stu['id'] ?></td><td><?=$stu['course'] ?></td><td><?php echo $stu['name'] ?></td><td><?php echo $stu['score'] ?></td></tr><?php endforeach?></tbody><tbody><?php// 对成绩进行判断,满足条件输出,不满足条件不输出 数据过滤 替代语法foreach($stus as $stu ) : ?><tr><!-- 将 { 用 冒号: 代替 将} 用endif代替 --><?php if($stu['score'] >= 70) :?><td><?= $stu['id'] ?></td><td><?=$stu['course'] ?></td><td><?php echo $stu['name'] ?></td><td><?php echo $stu['score'] ?></td><?php endif ?></tr><?php endforeach?></tbody><!-- /**** 流程控制的替代语法/模板语法* 1. foreach - endforeach* 2. for -> endfor* 3. if -> endif* 4. switch -> endswitch*/ --></body></html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号