分支与循环、php模板语法与html混编技巧
发布于:2022-04-21 15:48:33
次阅读

<!-- 1. 实例演示分支与循环 2. 实例演示php模板语法与html混编技巧 --><?php$age = 28;$wait = $age-18;// 1. 单分支if($age >= 18) echo "welcome" . "<br/>";// 2. 双分支if($age > 18) echo "welcome". "<br/>";else echo "wait" . $wait ."year" . "<br/>";// 3. 多分支if($age>=18 && $age<25) echo "welcome" . "<br/>";elseif($age>=25) echo "Hi Dave" . "<br/>";else echo "wait" . $wait ."year" . "<br/>";// 4. 多分支语法糖:switchswitch(true){ case $age>=25: echo "Hello Dave" . "<br/>"; break; default: echo "wait" . $wait ."year" . "<br/>";}// 循环// while$i = 0;while($i<5){ echo "有".$i."只小猪"."<br/>"; $i++;}echo "<hr>";// do-while$i = 0;do{ echo "有".$i."只小猪"."<br/>"; $i++;}while($i<5);echo "<hr>";// forfor($i=0;$i<5;$i++){ echo "有".$i."只小猪"."<br/>";}

<?php $user_freshman=[ ["id"=>"0","name"=>"Frank","course"=>"java","score"=>"89"], ["id"=>"1","name"=>"John","course"=>"java","score"=>"89"], ["id"=>"2","name"=>"Jane","course"=>"java","score"=>"89"], ["id"=>"3","name"=>"Dave","course"=>"php","score"=>"99"], ["id"=>"4","name"=>"David","course"=>"C","score"=>"79"], ]; $user_sophomore=[ ["id"=>"0","name"=>"Frank","course"=>"python","score"=>"89"], ["id"=>"1","name"=>"John","course"=>"big data","score"=>"89"], ["id"=>"2","name"=>"Jane","course"=>"graph","score"=>"89"], ["id"=>"3","name"=>"Dave","course"=>"web","score"=>"99"], ["id"=>"4","name"=>"David","course"=>"C++","score"=>"79"], ];?><!DOCTYPE html><html lang="en"><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>php模板语法与html混编</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>学号</th> <th>姓名</th> <th>课程</th> <th>成绩</th> </tr> </thead> <tbody> <!-- 大一 --> <!-- foreach --> <?php $list=null; foreach($user_freshman as $users){ $table = "<tr>"; $table.="<th>{$users["id"]}</th>"; $table.="<th>{$users["name"]}</th>"; $table.="<th>{$users["course"]}</th>"; $table.="<th>{$users["score"]}</th>"; $table.= "</tr>"; $list.=$table; } echo $list ?> </tbody> </table> <table> <caption>大二成绩表</caption> <thead> <tr> <th>学号</th> <th>姓名</th> <th>课程</th> <th>成绩</th> </tr> </thead> <tbody> <!-- 大二 --> <?php foreach($user_sophomore as $users):?> <tr> <th><?= $users["id"]?></th> <th><?= $users["name"]?></th> <th><?= $users["course"]?></th> <th><?= $users["score"]?></th> </tr> <?php endforeach ?> </tbody> </table></body></html>