Blogger Information
Blog 36
fans 4
comment 3
visits 31564
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
7.22 foreach遍历数组 演示get和post的数据处理过程
大灰狼的博客
Original
1162 people have browsed it

数组遍历 foreach 

对定义的数组$namelist 通过foreach进行了遍历。实例代码中写了备注并进行了多种格式输出。

实例

<?php

//数组 2关联数组
$namelist = ['zs'=>'张飒', 5=>'汪小菲',9=> '李霞飞','ly'=> '刘勇'];
//$namelist['zs']; $namelist[5]; $namelist[9]; $namelist['ly'];   

?>

<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>7.22 遍历数组</title>
</head>
<body>

<h3>纯php输出</h3>
<!--纯php输出代码-->
<?php
	//数组遍历foreach (数组名 as 键 => 值) {} 纯php输出
	foreach ($namelist as $key => $value) {
		echo "我的键是:" . $key . '我的值是:' . $value . '<br/>';
	};
?>
<hr />
<h3>php+html混编输出 (代码有点乱 不理想)</h3>
<!--php+html混编输出 (有点乱 不理想)-->
<?php
echo "<ul>";	
foreach($namelist as $key=>$value){
	echo '<li> <a href="#">'.$key.':'.$value.'</a></li>';
}
echo "</ul>";	
?>
<hr />
<h3>将php 和html分离({}花括号 容易出现)</h3>
<!--将php 和html分离({}花括号 容易出现)-->
<ul>
	<?php foreach($namelist as $key=>$value){ ?>
	<li><a href=""><?php echo $key.':'. $value ?></a></li>
	<?php } ?>
</ul>
<hr />
<h3>将php 和html分离({}花括号 用:和endforeach 进行替换(推荐)</h3>
<!--将php 和html分离({}花括号 用:和endforeach 进行替换 实际项目使用此方法)-->
<ul>
	<?php foreach($namelist as $key=>$value): ?>
	<li><a href=""><?php echo $key.':'. $value ?></a></li>
	<?php endforeach ?>
</ul>

</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

================================分割线

超全局变量$_GET

创建了一个form表单 在当前页面进行了get方式提交和接收操作。
当form通过url以键值对提交email和password的2个数据后 当前页面的php通过超全局变量$_GET 进行接收 并通过isset()来判断是否被赋值来判断是否有传值进来。

1.png

实例

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>7.22超全局变量$_GET.php</title>
</head>
<body>
<form action="" method="get">
	<p>
		<label for="email">邮箱</label>
		<input type="text" name="email" id="email" value="<?php echo isset($_GET['email'])? $_GET['email']:'';?>"/>
	</p>
	<p>
		<label for="password">密码</label>
		<input type="password" name="password" id="password" value="<?php echo isset($_GET['password'])? $_GET['password']:'';?>"/>
	</p>
	<p>
		<button>点击登录</button>
	</p>
	
</form>	
</body>
</html>

<?php
//$_GET是超全局变量 通过url传递数据
//$_GET是一个数组
//gettype — 获取变量的类型
//print_r 可打印 数组 对象
//isset 判断一个变量是否被声明
echo "<pre>";
//打印
print_r($_GET);

if(isset($_GET['email'])){
	echo $_GET['email'];
}else {
	echo '';
}

echo '<hr> 通过三目运算代替if else <br>';
//三元运算符 也叫三目运算(通过三目运算代替if else)
echo isset($_GET['email'])? $_GET['email']:'';
echo "<br>";
echo isset($_GET['password'])? $_GET['password']:'';

echo "</pre>";
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


-------------------------------------------------------------分割线

超全局变量$_POST

不管是get还是post数据都是来自表单。只是post支持传送的数据更大 比如图片、文件等更大的数据量传输。客户端发送数据不再通过url 。数据同样以键值形式通过post请求发送表单数据。

2.png


实例

<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>7.22超全局变量$_POST.php</title>
</head>
<body>
<form action="" method="post">
	<p>
		<label for="email">邮箱</label>
		<input type="text" name="email" id="email" value="<?=$_POST['email']??'';?>"/>
	</p>
	<p>
		<label for="password">密码</label>
		<input type="password" name="password" id="password" value="<?=$_POST['password']??'';?>"/>
	</p>
	<p>
		<button>点击登录</button>
	</p>
	
</form>	
</body>
</html>

<?php
//$_GET是超全局变量 通过url传递数据
//$_GET是一个数组
//gettype — 获取变量的类型
//print_r 可打印 数组 对象
//isset 判断一个变量是否被声明
echo "<pre>";
//打印
print_r($_POST);

if(isset($_POST['email'])){
	echo $_POST['email'];
}else {
	echo '';
}

echo '<br> if else <hr> 通过三目运算代替if else<br>';
//三元运算符 也叫三目运算(通过三目运算代替if else)
echo isset($_POST['email'])? $_POST['email']:'';
echo "<br>";
echo isset($_POST['password'])? $_POST['password']:'';

var_dump(is_bool($_POST['email'])) ;
echo "</pre>";
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


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!