Blogger Information
Blog 11
fans 0
comment 0
visits 8057
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
循环遍历数组及登陆表单验证--PHP培训第十期线上班
宋明杰
Original
655 people have browsed it

1,for循环遍历关联数组,

  1. $chenjidan = ['xuehao' => '9号','xingming' => '张三','chengji' => '100分'];
  2. //将数组的指针复位,
  3. reset($chenjidan);
  4. //循环3条件,1,循环变量初始化,2,循环条件判断,3循环变量的改变。
  5. for($i = 0; $i < count($chenjidan); $i++){
  6. $k = key($chenjidan); //用一个变量接收下标值,
  7. $v = current($chenjidan);//用一个变量接收元素值。
  8. echo "$k => $v,<br>";
  9. next($chenjidan);//
  10. }

实例运行:

2,white循环关联数组:

  1. //将数组的指针复位,
  2. reset($chenjidan);
  3. $i = 0;
  4. while ($i < count($chenjidan)) {
  5. $k = key($chenjidan); //用一个变量接收下标值,
  6. $v = current($chenjidan);//用一个变量接收元素值。
  7. echo "$k => $v,<br>";
  8. // echo key($chenjidan) . '=>' . current($chenjidan) . '<br>'; //不通过变量,直接输出。
  9. next($chenjidan);//
  10. $i++;
  11. }
  12. ?>

运行实例:

3,数组指针

下标 0 1 2 3
9号 张三 语文 98分

下面的函数可以针对数组进行相应的操作:

  1. current() //取得数组当前指针所在单元的值;
  2. key() //取得数组当前指针所在单元的下标;
  3. reset() //将数组指针移到最前面的位置,并取得新位置上的值;
  4. next() //将数组指针移到下一个位置,并取得新位置上的值;
  5. prev() //将数组指针移到上一个位置,并取得新位置上的值;
  6. end() //将数组指针移到最后面的位置,并取得新位置上的值;

4,登陆表单:

HTML代码:

  1. <div class="zc">
  2. <h3>用户注册</h3>
  3. <form action="xuexi2.php" method="post" class="zc1">
  4. <span>
  5. <label for="yonghuming">用户名:</label>
  6. <input type="text" name="yonghuming" id="yonghuming" placeholder="用户名为数字和英文字母">
  7. </span>
  8. <span>
  9. <label for="mima">密码:</label>
  10. <input type="text" name="mima" id="mima" placeholder="请输入密码">
  11. </span>
  12. <span>
  13. <label for="chongfumima">重复密码:</label>
  14. <input type="text" name="chongfumima" id="chongfumima" placeholder="重复输入密码">
  15. </span>
  16. <span class="zc1a">
  17. <label for="male">性别:</label>
  18. <span class="zc2">
  19. <input type="radio" name="gender" id="male" value="male" required><label for="male"></label>
  20. <input type="radio" name="gender" id="female" value="female" required><label for="female"></label>
  21. </span>
  22. </span>
  23. <span>
  24. <label for="lianxiren">联系人:</label>
  25. <input type="text" name="lianxiren" id="lianxiren" placeholder="请输入正确的姓名">
  26. </span>
  27. <span>
  28. <label for="tel">电话号码:</label>
  29. <input type="text" name="tel" id="tel" placeholder="请输入正确的电话号码">
  30. </span>
  31. <button>提交</button>
  32. </form>
  33. </div>

运行实例:

PHP后台验证代码:

  1. <?php
  2. //读取表单所有提交的值
  3. echo '<pre>' . print_r($_POST, true) . '</pre>>';
  4. //读取表单提交的单一项值
  5. echo '用户名:' . $_POST['yonghuming'] . '<br>';
  6. //判断请求类型
  7. echo '请求类型:' . $_SERVER['REQUEST_METHOD'] . '<br>';
  8. //判断请求的值是否和提交的值一样:
  9. if($_SERVER['REQUEST_METHOD'] === 'POST'){
  10. //empty():空字符串,0.null,false,
  11. //判断值是否为空,并用一个变量取出相应提交的值:
  12. if(!empty($_POST['yonghuming'])) $yonghuming = $_POST['yonghuming'];
  13. if(!empty($_POST['mima'])) $mima = $_POST['mima'];
  14. if(!empty($_POST['chongfumima'])) $chongfumima = $_POST['chongfumima'];
  15. if(!empty($_POST['gender'])) $gender = $_POST['gender'];
  16. if(!empty($_POST['lianxiren'])) $lianxiren = $_POST['lianxiren'];
  17. if(!empty($_POST['tel'])) $tel = $_POST['tel'];
  18. if ($mima === $chongfumima) {
  19. // md5():32位随机字符串, sha1():40位随机字符串
  20. $mima = md5(sha1($chongfumima));
  21. } else {
  22. exit('<script>alert("二次密码不一致");history.back();</script>');
  23. }
  24. //将上面变量组装成数组(compact)
  25. $zhuce = compact('yonghuming','mima','gender','lianxiren','tel');
  26. echo '<pre>' . print_r($zhuce, true) . '</pre>>';
  27. }
  28. else{
  29. echo ('<b style="color: red">类型错误</b>');

运行实例:

手写代码:

总结:

1,一步慢,步步慢,等抓紧时间追赶上进度。
2,对遍历数组要多熟悉
3,对函数要多熟悉,争取牢记于心。

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