博客列表 >foreach与它的替代语法,输出数组内容,GET/POST数据处理过程-2019年7月22日

foreach与它的替代语法,输出数组内容,GET/POST数据处理过程-2019年7月22日

blog
blog原创
2019年07月23日 16:57:15586浏览

一.foreach与它的替代语法,输出数组内容

1)

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>POST</title>
    <style>
        table tr th{
            width:60px;
            background-color:lightsalmon;
        }
        table tr td{
            text-align:center;
        }
    </style>
</head>
<body>
<?php
$number=[[11,22,33,44,55],[22,33,44,55,66],[33,44,55,66,77],[44,55,66,77,88],[55,66,77,88,99]];
?>
<table border="1px solid black" cellpadding="10" cellspacing="0">
    <thead>
    <tr>
        <th>id</th>
        <th>1</th>
        <th>2</th>
        <th>3</th>
        <th>4</th>
        <th>5</th>
    </tr>
    </thead>
    <tbody>
    <?php
        foreach($number as $key=>$value){
    ?>
    <tr>
        <td><?php echo ($key+1) ?></td>
        <td><?php echo $value['0']?></td>
        <td><?php echo $value['1']?></td>
        <td><?php echo $value['2']?></td>
        <td><?php echo $value['3']?></td>
        <td><?php echo $value['4']?></td>
    </tr>
    <?php } ?>
    </tbody>
</table>

</body>
</html>

运行实例 »

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

1.png

2)使用foreach替代语法,, 将大括号用冒号代替

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>POST</title>
    <style>
        table tr th{
            width:60px;
            background-color:lightsalmon;
        }
        table tr td{
            text-align:center;
        }
    </style>
</head>
<body>
<?php
$number=[[11,22,33,44,55],[22,33,44,55,66],[33,44,55,66,77],[44,55,66,77,88],[55,66,77,88,99]];
?>
<table border="1px solid black" cellpadding="10" cellspacing="0">
    <thead>
    <tr>
        <th>id</th>
        <th>1</th>
        <th>2</th>
        <th>3</th>
        <th>4</th>
        <th>5</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach($number as $key=>$value): ?>
        <tr>
            <td><?php echo ($key+1) ?></td>
            <td><?php echo $value['0']?></td>
            <td><?php echo $value['1']?></td>
            <td><?php echo $value['2']?></td>
            <td><?php echo $value['3']?></td>
            <td><?php echo $value['4']?></td>
        </tr>
    <?php endforeach ?>
    </tbody>
</table>

</body>
</html>

运行实例 »

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

1.png

二.GET/POST数据处理过程

1)get

实例

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

</body>
</html>
<?php
echo "<pre>";
print_r($_GET);
?>

运行实例 »

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

1.png

我们查看下数据包

1.png

可以发现get请求参数以键值对的方式,附加到url地址上,以&符号分割

2)post

实例

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

</body>
</html>
<?php
echo "<pre>";
print_r($_POST);
?>

运行实例 »

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

1.png

我们查看下数据包

1.png

可以看到请求的数据在header请求头中被发送出去, url地址看不到请求参数

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议