Blogger Information
Blog 22
fans 3
comment 3
visits 16504
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
遍历数组与get/post传参-2019年7月23日15点42分
辰晨的博客
Original
1095 people have browsed it

一、foreach遍历数组

foreach($数组名 as $键名=>$键值){ 内容 }

2.JPG

<?php
	$major = 'PHP工程师';
	$course = ['html','css','javascript','mysql','php'];
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>foreach循环数组</title>
</head>
<body>
	<table cellpadding="10" cellspacing="0" border="1">
		<tr>
			<th colspan="5"><?php echo $major; ?></th>
		</tr>
		<tr>
			<?php foreach($course as $key=>$vaule){ ?>
				<td><?php echo $vaule; ?></td>
			<?php } ?>
		</tr>
	</table>
</body>
</html>

运行实例 »

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


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>foreach循环数组</title>
</head>
<body>
	<table cellpadding="10" cellspacing="0" border="1">
		<tr>
			<th colspan="5"><?php echo $major; ?></th>
		</tr>
		<tr>
			<?php foreach($course as $course): ?>
				<td><?php echo $course; ?></td>
			<?php endforeach; ?>
		</tr>
	</table>
</body>
</html>

运行实例 »

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


二、get传参

通过url地址栏把键值对传送到服务器

get.JPG

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>get传参</title>
</head>
<body>
	<form action="" method="get">
	<p>
		<label for="username">用户名:</label>
		<input type="username" id="username" name="username" value="<?php echo isset($_GET['username']) ? $_GET['username'] : ''; ?>">
	</p>
	<p>
		<label for="email">邮   箱:</label>
		<input type="email" id="email" name="email" value="<?php echo isset($_GET['email']) ? $_GET['email'] : ''; ?>">
	</p>
	<p>
		<label for="password">密   码:</label>
		<input type="password" id="password" name="password" value="<?php echo isset($_GET['password']) ? $_GET['password'] : ''; ?>">
	</p>
	<p>
		<button>登录</button>
	</p>
	</form>
</body>
</html>

<?php
	echo '<pre>';
	print_r($_GET);
?>

运行实例 »

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

三、post传参

键值对在地址栏不显示,相对安全

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>POST传参</title>
</head>
<body>
	<form action="" method="post">
	<p>
		<label for="username">用户名:</label>
		<input type="username" id="username" name="username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : ''; ?>">
	</p>
	<p>
		<label for="email">邮   箱:</label>
		<input type="email" id="email" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>">
	</p>
	<p>
		<label for="password">密   码:</label>
		<input type="password" id="password" name="password" value="<?php echo isset($_POST['password']) ? $_POST['password'] : ''; ?>">
	</p>
	<p>
		<button>登录</button>
	</p>
	</form>
</body>
</html>

<?php
	echo '<pre>';
	print_r($_POST);
?>

运行实例 »

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


Correction status:qualified

Teacher's comments:<label for="email">邮 箱:</label>, 这里面的空格应该用html实体字符$nbsp;添加, 而不是直接硬空格
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