PHP循环控制,for,while遍历关联数组
一、PHP循环控制
数组指针
1、key()
:返回当前数组元素的键
2、current()
:返回当前数组元素的值
3、next()
:将指针指向数组元素当前元素的下一个元素的位置
4、reset()
:复位数组指针,指向第一个元素
5、prev()
:数组指针上移一个
6、end()
:数组指针移到最后一个元素
PHP循环控制 、for()
、while()
遍历数组
<?php
/*php循环控制-遍历索引数组*/
$arr1 = ['van', 'toan', 'anh', 'ly', 'hoa hoc'];
//for()
for($i = 0; $i < count($arr1); $i++ ) {
echo $arr1[$i] .',';
}
echo '<hr>';
//while()
$i = 0;
while ($i < count($arr1)) {
echo $arr1[$i] .',';
$i++;
}
echo '<hr>';
//foreach
foreach($arr1 as $value) {
echo $value . '<br>';
}
echo '<hr>';
/*for()、while()遍历关联数组*/
$arr2 = ['quocgia' =>'vietnam', 'thudo' => 'hanoi', 'dantoc' => 'kinh', 'danso' => '100000000', 'trungtam' => 'haiphong'];
//for()
for($i = 0; $i < count($arr2); $i++){
echo key($arr2) . '==>' . current($arr2) . '<br>';
next($arr2);
}
echo '<hr>';
//while()
reset($arr2);
while (current($arr2)) {
echo key($arr2) . '+++>' . current($arr2) . '<br>';
next($arr2);
}
echo '<hr>';
//foreach()
foreach ($arr2 as $key => $value) {
echo "$key => $value <br>";
}
echo '<hr>';
二、表单验证
HTML代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>表单验证</title>
<style>
.register > form{
width: 400px;
height: 300px;
background-color: #19c0e7;
border-radius: 10px; /*圆角*/
text-align: center;
margin: auto;
}
.register > form > div {
display: grid;
grid-template-columns: 100px 200px;
margin: 20px 0;
grid-column-gap:10px;
}
.register > form > div > label{
text-align: right;
}
.register > form > div > input{
border-radius: 5px;
}
</style>
</head>
<body>
<div class="register">
<form action="action.php" method="post">
<h3>用户注册</h3>
<div>
<label for="username">用户名</label>
<input type="text" name="username" id="username" placeholder="不超过20个字符" required autofocus>
</div>
<div>
<label for="password1">密码</label>
<input type="password" name="password1" id="password1" placeholder="不能为空" required>
</div>
<div>
<label for="password2">重复密码</label>
<input type="password" name="password2" id="password2" placeholder="再次输入的密码必须一致。" required>
</div>
<div>
<label for="email">邮箱</label>
<input type="email" name="email" id="email" placeholder="不能为空" required>
</div>
<span>
<button>提交</button>
</span>
</form>
</div>
</body>
</html>
HTML效果
PHP代码:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') { /*判断请求类型*/
// echo 'YES';
//empty():判断一个变量是否为“空”,当一个变量值为0,empty() 认为这个变量同等于空,即相当于没有设置。
if(!empty($_POST['username'])) $username = $_POST['username'];
if(!empty($_POST['password1'])) $password1 = $_POST['password1'];
if(!empty($_POST['password2'])) $password2 = $_POST['password2'];
if(!empty($_POST['email'])) $email = $_POST['email'];
if ($password1 === $password2) {
$password = md5(sha1($password1));
}else{
exit('<script>alert("两次密码不一致。"); history.back();</script>');
}
//compact():将得到的变量压缩到一个数组
$data = compact('username', 'password', 'email');
if((print($data)) === 1){
exit('<script>alert("注册成功,请登录。"); history.back();</script>');
}else{
exit('<script>alert("注册失败,请登重试。"); history.back();</script>');
}
}else{
exit('<h3 style="color: red">请求类型错误!</h3>');
}