Blogger Information
Blog 42
fans 0
comment 0
visits 15642
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0809分支与循环,流程控制之模板语法
小言
Original
309 people have browsed it

分支分类以下三种类型

1.单分支

  1. if (XXX){
  2. echo 'XXXXX';
  3. }

单分支就是以if,填充条件,从而返回结果

2.双分支 :三元

  1. if ($age >= 18){
  2. echo '已成年,可以观看<br>';
  3. }else{
  4. echo '未成年,请在父母陪同下观看 <br>';
  5. }

双分支和单分支 不同的地方在于,多了一个else ,从而判断输出不同的内容,如果是满足则输出if。不满足的情况下,输出else

3.多分支 :switch

  1. switch (true){
  2. case $age >= 18 && $age < 30;
  3. echo "{$age}岁,可以按揭?<br>";
  4. break;
  5. case $age >= 30 && $age < 45;
  6. echo "{$age}岁,可以按揭?<br>";
  7. break;
  8. default;
  9. echo "{$age}放学送你回家<br>";
  10. }

循环

  1. $colors = ['red', 'green', 'blue'];
  2. $i = 0;
  3. $list = '<ul style="border:1px solid red">';
  4. if ($i < count($colors)){
  5. $list .= "<li>{$colors[$i]}</li>";
  6. }
  7. //$i += 1;
  8. $i++;
  9. if ($i < count($colors)){
  10. $list .= "<li>{$colors[$i]}</li>";
  11. }
  12. $i++;
  13. if ($i < count($colors)){
  14. $list .= "<li>{$colors[$i]}</li>";
  15. }
  16. $list .= '</ul>';
  17. echo $list;

模板语法

  1. <?php
  2. $aoyun = [
  3. ['id' => 1, 'name' => '中国', 'score' => 83],
  4. ['id' => 2, 'name' => '法国', 'score' => 75],
  5. ['id' => 3, 'name' => '德国', 'score' => 52],
  6. ['id' => 4, 'name' => '英国', 'score' => 88],
  7. ['id' => 5, 'name' => '美国', 'score' => 65],
  8. ['id' => 6, 'name' => '俄罗斯', 'score' => 53],
  9. ['id' => 7, 'name' => '日本', 'score' => 63],
  10. ['id' => 8, 'name' => '奥大利亚', 'score' => 77],
  11. ['id' => 9, 'name' => '西班牙', 'score' => 93],
  12. ['id' => 10, 'name' => '越南', 'score' => 41],
  13. ]
  14. ?>
  1. <?php
  2. foreach($aoyun as $ay) : ?>
  3. <tr>
  4. <?php if ($ay['score'] >= 70) : ?>
  5. <td><?=$ay['id']?></td>
  6. <td><?=$ay['name']?></td>
  7. <td><?=$ay['score']?></td>
  8. <?php endif ?>
  9. </tr>
  10. <?php endforeach ?>

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!