Blogger Information
Blog 38
fans 1
comment 0
visits 28662
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP流程控制(条件判断、循环和跳出流程语句)以及GET传值的练习 - 九期线上班
fkkf467
Original
919 people have browsed it

一、PHP条件判断

1. 三元运算符 ? :

  1. <?php
  2. $money = 100;
  3. echo $money >= 100 ? '买糖' : '不买糖';
  4. ?>


2. if

  1. <?php
  2. $money = 150;
  3. if ($money >= 100){
  4. echo '出去耍';
  5. }
  6. ?>


3. if else

  1. <?php
  2. $money = 10;
  3. if ($money >= 100){
  4. echo '出去耍';
  5. }else{
  6. echo '还是在家睡觉吧';
  7. }
  8. ?>


4. if elseif else

  1. <?php
  2. $score = 76;
  3. if ($score == 100){
  4. echo '满分';
  5. }elseif ($score >= 80){
  6. echo '优秀';
  7. }elseif ($score >= 70){
  8. echo '良好';
  9. }elseif ($score >= 60){
  10. echo '及格';
  11. }else{
  12. echo '不及格';
  13. }
  14. ?>


5. switch case default

  1. <?php
  2. $score = 43;
  3. switch ($score){
  4. case 100:
  5. echo '满分';
  6. break;
  7. case $score >= 90:
  8. echo '优秀';
  9. break;
  10. case $score >= 70:
  11. echo '良好';
  12. break;
  13. case $score >= 60:
  14. echo '及格';
  15. break;
  16. default:
  17. echo '不及格';
  18. break;
  19. }
  20. ?>

二、PHP循环

1. while

  1. <?php
  2. $i = 0;
  3. while ($i < 10){
  4. echo $i . '<hr>';
  5. $i++;
  6. }
  7. ?>

2. do while

  1. <?php
  2. $i = 1;
  3. do{
  4. echo $i;
  5. echo '<hr>';
  6. $i++;
  7. }while($i<1);
  8. ?>


3. for

  1. <?php
  2. for ($i=0;$i<10;$i++) {
  3. echo $i;
  4. echo '<hr>';
  5. }
  6. ?>

三、跳出流程语句

1. continue

  1. <?php
  2. for ($i=1;$i<10;$i++) {
  3. if($i == 4){
  4. continue;
  5. }
  6. echo $i;
  7. echo '<hr>';
  8. }
  9. ?>


2. break

  1. <?php
  2. for ($i=1;$i<10;$i++) {
  3. if($i == 4){
  4. break;
  5. }
  6. echo $i;
  7. echo '<hr>';
  8. }
  9. ?>


四、GET传值

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>GET传值</title>
  6. </head>
  7. <body>
  8. <form action="" method="get">
  9. <label for="email">邮箱:</label>
  10. <input type="email" id="email" name="email" value="">
  11. <br>
  12. <label for="password">密码:</label>
  13. <input type="password" id="password" name="password" value="">
  14. <br>
  15. <button>登录</button>
  16. </form>
  17. </body>
  18. </html>
  19. <?php
  20. print_r($_GET);
  21. echo '<br>';
  22. echo $_GET['email'];
  23. echo '<br>';
  24. if (isset($_GET['password'])) {
  25. echo $_GET['password'];
  26. }else{
  27. $_GET['password'] = '';
  28. }
  29. echo '<br>';
  30. echo isset($_GET['email']) ? $_GET['email'] : '';
  31. echo '<pre>';
  32. print_r($_GET);
  33. ?>

五、综合练习

header.php

  1. <?php
  2. $system = [
  3. 'name' => '影视大全',
  4. 'copy' => '猪小明'
  5. ];
  6. $cates = [
  7. [
  8. 'cate_id' => 1,
  9. 'name' => 'rm',
  10. 'alias' => '热门'
  11. ],
  12. [
  13. 'cate_id' => 2,
  14. 'name' => 'zx',
  15. 'alias' => '最新'
  16. ],
  17. [
  18. 'cate_id' => 3,
  19. 'name' => 'dbgf',
  20. 'alias' => '豆瓣高分'
  21. ],
  22. [
  23. 'cate_id' => 4,
  24. 'name' => 'lmjp',
  25. 'alias' => '冷门佳片'
  26. ],
  27. [
  28. 'cate_id' => 5,
  29. 'name' => 'hy',
  30. 'alias' => '华语'
  31. ],
  32. [
  33. 'cate_id' => 6,
  34. 'name' => 'om',
  35. 'alias' => '欧美'
  36. ],
  37. [
  38. 'cate_id' => 7,
  39. 'name' => 'hg',
  40. 'alias' => '韩国'
  41. ],
  42. [
  43. 'cate_id' => 8,
  44. 'name' => 'rb',
  45. 'alias' => '日本'
  46. ]
  47. ];
  48. $movies = [
  49. [
  50. 'cate_id' => 1,
  51. 'mov_id' => 1,
  52. 'name' => '小丑',
  53. 'image' => 'rm1.jpg',
  54. 'video' => 'rm1.mp4',
  55. 'direct' => '托德·菲利普斯',
  56. 'write' => '托德·菲利普斯 斯科特·西尔弗',
  57. 'star' => '杰昆·菲尼克斯 罗伯特·德尼罗',
  58. 'type' => '剧情 惊悚 犯罪',
  59. 'area' => '加拿大 美国',
  60. 'language' => '英语',
  61. 'date' => '2019-10-04',
  62. 'length' => '122分钟',
  63. 'detail' => '电影《小丑》以同名DC漫画角色为基础,由华纳兄弟影业公司发行,计划于2019年10月4日上映。本片的故事将独立于DCEU之外,故事背景设置在20世纪80年代,讲述了一位生活陷入困境的脱口秀喜剧演员渐渐走向精神的崩溃,在哥谭市开始了疯狂的犯罪生涯,最终成为了蝙蝠侠的宿敌“小丑”的故事。'
  64. ],
  65. [
  66. 'cate_id' => 1,
  67. 'mov_id' => 2,
  68. 'name' => '小小的愿望',
  69. 'image' => 'rm2.jpg',
  70. 'video' => 'rm2.mp4',
  71. 'direct' => '田羽生',
  72. 'write' => '田羽生 大宽 大广',
  73. 'star' => '彭昱畅 王大陆 魏大勋',
  74. 'type' => '剧情 喜剧',
  75. 'area' => '中国大陆',
  76. 'language' => '汉语普通话',
  77. 'date' => '2019-09-12',
  78. 'length' => '107分钟',
  79. 'detail' => '突然患上肌肉萎缩症的高中生高远(彭昱畅 饰),被医生告知时日不多,决心在临死之前完成一个愿望,那就是成为一个真正的男人!得知这个消息的身边两位好兄弟徐浩(王大陆 饰)和张正阳(魏大勋 饰),为了兄弟最后的愿望,不惜一切奋战到底!'
  80. ],
  81. [
  82. 'cate_id' => 1,
  83. 'mov_id' => 3,
  84. 'name' => '最普通的恋爱',
  85. 'image' => 'rm3.jpg',
  86. 'video' => 'rm1.mp4',
  87. 'direct' => '金涵潔',
  88. 'write' => '金涵潔',
  89. 'star' => ' 金来沅 孔晓振 姜其永',
  90. 'type' => '喜剧 爱情',
  91. 'area' => '韩国',
  92. 'language' => '韩语',
  93. 'date' => '2019-10-02',
  94. 'length' => '109分钟',
  95. 'detail' => 'hanshi.tv电影《最普通的恋爱》将是一部浪漫喜剧,讲述30代男女间的现实爱情,将于今年冬季进行拍摄,预计将于2019年下半年于韩国上映。'
  96. ],
  97. [
  98. 'cate_id' => 1,
  99. 'mov_id' => 4,
  100. 'name' => '罗小黑战记',
  101. 'image' => 'rm4.jpg',
  102. 'video' => 'rm4.mp4',
  103. 'direct' => '木头',
  104. 'write' => '木头',
  105. 'star' => '山新 郝祥海 刘明月',
  106. 'type' => '动作 动画 奇幻',
  107. 'area' => '中国大陆',
  108. 'language' => '汉语普通话',
  109. 'date' => '2019-09-07',
  110. 'length' => '101分钟',
  111. 'detail' => '在熙攘的人类世界里,很多妖精隐匿其中,他们与人类相安无事地生活着。猫妖罗小黑因为家园被破坏,开始了它的流浪之旅。这场旅途中惺惺相惜的妖精同类与和谐包容的人类伙伴相继出现,让小黑陷入了两难抉择,究竟何处才是真正的归属?'
  112. ],
  113. // 省略部分
  114. ];
  115. ?>
  116. <!doctype html>
  117. <html lang="en">
  118. <head>
  119. <meta charset="UTF-8">
  120. <title><?php echo $system['name'];?></title>
  121. <link rel="stylesheet" href="static/css/style.css">
  122. </head>
  123. <body>
  124. <header>
  125. <div class="nav">
  126. <a href="index.php">首页</a>
  127. <?php
  128. foreach ($cates as $cate) {
  129. echo "<a href='list.php?cate_id=" . $cate['cate_id'] . "'>{$cate['alias']}</a>";
  130. }
  131. ?>
  132. </div>
  133. </header>
  134. <main>

footer.php

  1. <?php
  2. $system = [
  3. 'name' => '影视大全',
  4. 'copy' => '猪小明'
  5. ];
  6. ?>
  7. </main>
  8. <footer>
  9. <p><?php echo $system['copy'];?> &copy; 版权所有</p>
  10. </footer>
  11. </body>
  12. </html>

index.php

  1. <?php
  2. include __DIR__ . '/header.php';
  3. foreach ($cates as $cate) {
  4. echo "<article><h3>{$cate['alias']}</h3><ul>";
  5. $i = 0;
  6. foreach ($movies as $movie) {
  7. if ($movie['cate_id'] === $cate['cate_id']) {
  8. $i++;
  9. echo "<li><a href='detail.php?mov_id=" . $movie['mov_id'] . "'><img src='static/images/" . $movie['image'] . "' alt=''><span>{$movie['name']}<i>8.9</i></span></a></li>";
  10. if ($i == 5) {
  11. echo '</ul><ul>';
  12. }
  13. }
  14. }
  15. echo '</ul></article>';
  16. }
  17. include __DIR__ . '/footer.php';


list.php

  1. <?php
  2. include __DIR__ . '/header.php';
  3. $cate_id = intval($_GET['cate_id']);
  4. foreach ($cates as $cate) {
  5. if ($cate_id === $cate['cate_id']) {
  6. echo "<article><h3>{$cate['alias']}</h3><ul>";
  7. $i = 0;
  8. foreach ($movies as $movie) {
  9. if ($movie['cate_id'] === $cate['cate_id']) {
  10. $i++;
  11. echo "<li><a href='detail.php?mov_id=" . $movie['mov_id'] . "'><img src='static/images/" . $movie['image'] . "' alt=''><span>{$movie['name']}<i>8.9</i></span></a></li>";
  12. if ($i == 5) {
  13. echo '</ul><ul>';
  14. }
  15. }
  16. }
  17. echo '</ul></article>';
  18. }
  19. }
  20. include __DIR__ . '/footer.php';


detail.php

  1. <?php
  2. include __DIR__ . '/header.php';
  3. $mov_id = intval($_GET['mov_id']);
  4. foreach ($movies as $movie) {
  5. if ($mov_id === $movie['mov_id']){
  6. echo "<h2>{$movie['name']}</h2>";
  7. echo '<div class="video">';
  8. echo '<video src="static/video/' . $movie['video'] . '" autoplay="autoplay" controls="controls" width="800"></video><ul>';
  9. echo '<li><span>导演:</span><a href="">' . $movie['direct'] . '</a></li>';
  10. echo '<li><span>编剧:</span><a href="">' . $movie['write'] . '</a></li>';
  11. echo '<li><span>主演:</span><a href="">' . $movie['star'] . '</a></li>';
  12. echo '<li><span>类型:' . $movie['type'] . '</span></li>';
  13. echo '<li><span>地区:' . $movie['area'] . '</span></li>';
  14. echo '<li><span>语言:' . $movie['language'] . '</span></li>';
  15. echo '<li><span>上映时间:' . $movie['date'] . '</span></li>';
  16. echo '<li><span>时长:' . $movie['length'] . '</span></li></ul></div><div class="detail">';
  17. echo "<h2>{$movie['name']}的剧情简介 · · · · · ·</h2><p>{$movie['detail']}</p></div>";
  18. }
  19. }
  20. include __DIR__ . '/footer.php';

六、总结

学会了php的判断和循环,还掌握了GET传值。

Correcting teacher:天蓬老师天蓬老师

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