Blogger Information
Blog 25
fans 1
comment 1
visits 17092
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0108作业+循环控制之for,while,表达验证+10期线上班
江川林
Original
555 people have browsed it

抱歉,朱老师,作业交的比较迟,本来想把大作业做了一起交,还是先把之前的作业交了!(#^.^#)

for循环与while循环

for与while循环之索引数组和关联数组的遍历

  1. <?php
  2. //for()循环遍历所以索引数组
  3. $arr1 = ['吃饭','睡觉','打豆豆'];
  4. $result = ' ' ;
  5. for ($i = 0 ; $i < count($arr1) ; $i++ ) {
  6. $result .= $arr1[$i] . '<br>';
  7. }
  8. echo $result. '<br>';
  9. //die();
  10. //while()循环遍历所以索引数组
  11. //入口判断型
  12. $i = 0 ;
  13. $result = ' ' ;
  14. while ($i < count($arr1) ) {
  15. $result .= $arr1[$i] . '<br>';
  16. $i++;
  17. }
  18. echo $result;
  19. //出口判断型
  20. $i = 0 ;
  21. $result = ' ' ;
  22. do{
  23. $result .= $arr1[$i];
  24. $i++;
  25. }while( $i > count($arr1));
  26. echo $result . '<br>';
  27. echo '<hr>';
  28. // for()循环遍历关联数组
  29. $arr2 = [ 'id' =>111,'name'=>'小江','height'=>'180cm'];
  30. $result = ' ' ;
  31. reset($arr2);
  32. for ($i = 0 ; $i < count($arr2) ; $i++ ) {
  33. echo key($arr2).'=>'. current($arr2).'<br>';
  34. next($arr2);
  35. }
  36. echo '<br>';
  37. //while ()遍历关联数组
  38. reset($arr2);
  39. $i = 0 ;
  40. while (current($arr2)){
  41. echo key($arr2).'=>'. current($arr2);
  42. next($arr2);
  43. }
  44. //foreach ()遍历数组
  45. foreach ($arr2 as $key => $current) {
  46. echo "$key => $current" . '<br>';
  47. }

但是一定要注意

-在遍历关联数组的时候,一定要记住以下几个函数:
-1,reset($array):重置数组指针,指向第一个键
-2,next($array):将数组指针向后移一个
-3,prev($array):将数组指针向前移一个
-4,end($array):将数组指针移向末尾

-while循环的细节注意:

  1. //初始条件在while外面
  2. $i = 0 ;
  3. $result = ' ' ;
  4. while ($i < count($arr1) ) {
  5. $result .= $arr1[$i] . '<br>';
  6. //循环更新条件必须在这个位置
  7. $i++;
  8. }
  9. echo $result;

以下是PHP表单验证图片


源代码——HTML

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>注册表单</title>
  6. <style>
  7. * {
  8. padding: 0;
  9. margin: 0;
  10. /*outline: 1px solid red;*/
  11. }
  12. body {
  13. width: 1200px;
  14. margin: auto;
  15. display: flex;
  16. /*align-content: center;*/
  17. }
  18. .login {
  19. height: 300px;
  20. width: 300px;
  21. margin: 100px auto 0;
  22. }
  23. .login > h2 {
  24. text-align: center;
  25. }
  26. .login > .form {
  27. height: 100%;
  28. display: flex;
  29. flex-grow: 1;
  30. flex-flow: column nowrap;
  31. justify-content: space-evenly;
  32. /*align-: space-evenly;*/
  33. background-color: lightskyblue;
  34. }
  35. .login > .form {
  36. border-radius: 10px;
  37. }
  38. .login > .form:hover {
  39. box-shadow: 0 0 3px #333333;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <div class="login">
  45. <h2>用户注册</h2>
  46. <form action="demo2.php" method="post" class="form">
  47. <section class="name">
  48. <label for="name">姓名:</label>
  49. <input type="text" name="name" id="name" placeholder="请输入您的真实姓名">
  50. </section>
  51. <section class="cellphone">
  52. <label for="cellphone">电话:</label>
  53. <input type="text" name="cellphone" id="cellphone" placeholder="请输入您的电话号码">
  54. </section>
  55. <section class="sex">
  56. <label for="male">性别:</label>
  57. <input type="radio" name="sex" id="male" value="male" checked><label for="male"></label>
  58. <input type="radio" name="sex" id="female" value="female"><label for="female"></label>
  59. </section>
  60. <section>
  61. 爱好: <input type="checkbox" name="hobby" id="feed" value="feed"><label for="feed">吃饭</label>
  62. <input type="checkbox" name="hobby" id="sleep" value="sleep" checked><label for="sleep">睡觉</label>
  63. <input type="checkbox" name="" id="hobby" value="fight"><label for="fight">打豆豆</label><br>
  64. </section>
  65. <section>
  66. <button>提交</button>
  67. <button type="reset">重置</button>
  68. </section>
  69. </form>
  70. </div>
  71. </body>
  72. </html>

以下是PHP验证代码

  1. <?php
  2. //超全局变量
  3. //$_REQUEST,里面保存的是用户所以的请求类型
  4. //echo '<pre>' . print_r($_REQUEST,true) . '</pre>';
  5. //判断用户的请求类型是否合法
  6. //echo '请求类型:' . $_SERVER['REQUEST_METHOD'] . '<br>';
  7. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  8. // 判断请求变量是否设置,并且值不能为NULL
  9. // empty()空字符串,0,NULL,false
  10. if (!empty($_POST['name'])) $name = $_POST['name'];
  11. if (!empty($_POST['cellphone'])) $cellphone = $_POST['cellphone'];
  12. if (!empty($_POST['sex'])) $sex = $_POST['sex'];
  13. if (!empty($_POST['hobby'])) $hobby = $_POST['hobby'];
  14. // 将以上信息封装到字符串中
  15. $login = compact('name' , 'cellphone' , 'sex' , 'hobby' );
  16. echo '<pre>' . print_r($login,true) . '</pre>';
  17. }else {
  18. exit('<h3 style="color: red">请求的变量错误</h3>');
  19. }

大概的验证逻辑思路:

-1,验证HTML页面转过来的请求类型是否合法,请求类型是否是POST
$_SERVER['REQUEST_METHOD']
-2,然后获取表单提交过来的信息
$_POST['name']
-3,封装获取的信息
compact()

以下是手写版

有一张怎么都上传不起。。。。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!