Blogger Information
Blog 119
fans 3
comment 1
visits 94331
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP基础 循环控制&表单验证----PHP培训十期线上班 学号:510251 01月08日作业
赵大叔
Original
759 people have browsed it

PHP循环控制,for,while遍历关联数组

一、PHP循环控制

数组指针

1、key():返回当前数组元素的键
2、current():返回当前数组元素的值
3、next():将指针指向数组元素当前元素的下一个元素的位置
4、reset():复位数组指针,指向第一个元素
5、prev():数组指针上移一个
6、end():数组指针移到最后一个元素

PHP循环控制 、for()while()遍历数组

  1. <?php
  2. /*php循环控制-遍历索引数组*/
  3. $arr1 = ['van', 'toan', 'anh', 'ly', 'hoa hoc'];
  4. //for()
  5. for($i = 0; $i < count($arr1); $i++ ) {
  6. echo $arr1[$i] .',';
  7. }
  8. echo '<hr>';
  9. //while()
  10. $i = 0;
  11. while ($i < count($arr1)) {
  12. echo $arr1[$i] .',';
  13. $i++;
  14. }
  15. echo '<hr>';
  16. //foreach
  17. foreach($arr1 as $value) {
  18. echo $value . '<br>';
  19. }
  20. echo '<hr>';
  21. /*for()、while()遍历关联数组*/
  22. $arr2 = ['quocgia' =>'vietnam', 'thudo' => 'hanoi', 'dantoc' => 'kinh', 'danso' => '100000000', 'trungtam' => 'haiphong'];
  23. //for()
  24. for($i = 0; $i < count($arr2); $i++){
  25. echo key($arr2) . '==>' . current($arr2) . '<br>';
  26. next($arr2);
  27. }
  28. echo '<hr>';
  29. //while()
  30. reset($arr2);
  31. while (current($arr2)) {
  32. echo key($arr2) . '+++>' . current($arr2) . '<br>';
  33. next($arr2);
  34. }
  35. echo '<hr>';
  36. //foreach()
  37. foreach ($arr2 as $key => $value) {
  38. echo "$key => $value <br>";
  39. }
  40. echo '<hr>';


二、表单验证

HTML代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>表单验证</title>
  6. <style>
  7. .register > form{
  8. width: 400px;
  9. height: 300px;
  10. background-color: #19c0e7;
  11. border-radius: 10px; /*圆角*/
  12. text-align: center;
  13. margin: auto;
  14. }
  15. .register > form > div {
  16. display: grid;
  17. grid-template-columns: 100px 200px;
  18. margin: 20px 0;
  19. grid-column-gap:10px;
  20. }
  21. .register > form > div > label{
  22. text-align: right;
  23. }
  24. .register > form > div > input{
  25. border-radius: 5px;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div class="register">
  31. <form action="action.php" method="post">
  32. <h3>用户注册</h3>
  33. <div>
  34. <label for="username">用户名</label>
  35. <input type="text" name="username" id="username" placeholder="不超过20个字符" required autofocus>
  36. </div>
  37. <div>
  38. <label for="password1">密码</label>
  39. <input type="password" name="password1" id="password1" placeholder="不能为空" required>
  40. </div>
  41. <div>
  42. <label for="password2">重复密码</label>
  43. <input type="password" name="password2" id="password2" placeholder="再次输入的密码必须一致。" required>
  44. </div>
  45. <div>
  46. <label for="email">邮箱</label>
  47. <input type="email" name="email" id="email" placeholder="不能为空" required>
  48. </div>
  49. <span>
  50. <button>提交</button>
  51. </span>
  52. </form>
  53. </div>
  54. </body>
  55. </html>

HTML效果

PHP代码:

  1. <?php
  2. if ($_SERVER['REQUEST_METHOD'] === 'POST') { /*判断请求类型*/
  3. // echo 'YES';
  4. //empty():判断一个变量是否为“空”,当一个变量值为0,empty() 认为这个变量同等于空,即相当于没有设置。
  5. if(!empty($_POST['username'])) $username = $_POST['username'];
  6. if(!empty($_POST['password1'])) $password1 = $_POST['password1'];
  7. if(!empty($_POST['password2'])) $password2 = $_POST['password2'];
  8. if(!empty($_POST['email'])) $email = $_POST['email'];
  9. if ($password1 === $password2) {
  10. $password = md5(sha1($password1));
  11. }else{
  12. exit('<script>alert("两次密码不一致。"); history.back();</script>');
  13. }
  14. //compact():将得到的变量压缩到一个数组
  15. $data = compact('username', 'password', 'email');
  16. if((print($data)) === 1){
  17. exit('<script>alert("注册成功,请登录。"); history.back();</script>');
  18. }else{
  19. exit('<script>alert("注册失败,请登重试。"); history.back();</script>');
  20. }
  21. }else{
  22. exit('<h3 style="color: red">请求类型错误!</h3>');
  23. }

效果

手抄作业

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
Author's latest blog post