Blogger Information
Blog 26
fans 0
comment 0
visits 11927
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php 使用流程控制替换语法, 替换HTML 循环列表(1124)
高空中的云
Original
580 people have browsed it

在html中写php,用到流程控制的替换语法

原则是用冒号:替换掉{}

知识点

  1. if (true) {
  2. echo 'xxx';
  3. } else {
  4. echo 'xxxx';
  5. }
  6. // 替换语法
  7. if (true) :
  8. echo 'xxx';
  9. else :
  10. echo 'xxxx';
  11. endif;
  12. if (true) :
  13. echo 'xxx';
  14. elseif (true) :
  15. echo 'xxxx';
  16. elseif (true) :
  17. echo 'xxxx';
  18. endif;
  19. switch (true) :
  20. case 'xxx':
  21. //....
  22. break;
  23. case 'xxx':
  24. //....
  25. break;
  26. case 'xxx':
  27. //....
  28. break;
  29. default:
  30. //...
  31. endswitch;
  32. while (true) {
  33. //...
  34. }
  35. while (true) :
  36. //...
  37. endwhile;
  38. for (;;) {
  39. //....
  40. }
  41. for (;;) :
  42. //....
  43. endfor;
  44. foreach ($data as $k=>$v) {
  45. }
  46. foreach ($data as $k=>$v) :
  47. endforeach;

新闻列表

  1. <?php
  2. // 导入外部数据
  3. $news = require 'data/news.php';
  4. ?>
  5. <div class="news">
  6. <h3>新闻列表</h3>
  7. <div class="list">
  8. <?php foreach ($news as $new) :
  9. if($new['id'] <= 5) :
  10. ?>
  11. <a href="<?=$new['url'] ?>"><?=$new['title'] ?></a>
  12. <?php endif; endforeach ?>
  13. </div>
  14. </div>

产品列表

  1. <?php
  2. // 当然,也可以在循环体直接写for(i=1;i<=8;i++);不用在这里定义了
  3. $products = [1,2,3,4,5,6,7,8];
  4. ?>
  5. <div class="items">
  6. <h3>产品列表</h3>
  7. <div class="list">
  8. <?php foreach ($products as $product) : ?>
  9. <div class="item">
  10. <img src="static/images/item<?=$product ?>.jpeg" alt="" />
  11. <a href="">最新产品<?=$product ?></a>
  12. </div>
  13. <?php endforeach ?>
  14. </div>
  15. </div>
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!