Correction status:qualified
Teacher's comments:理解还可以
foreach:
在php中数组可使用foreach()语句输出
foreach( $array as $key=>$value) {...}
<?php $headline = '音乐'; $music = ['QQ音乐','酷狗音乐','酷我音乐']; ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title> <?php echo $headline ?> </title> </head> <body> <h2><?php echo $headline ?></h2> <ul> <li><a href=""><?php echo 'QQ音乐' ?></a></li> <li><a href=""><?php echo '酷狗音乐' ?></a></li> <li><a href=""><?php echo '酷我音乐' ?></a></li> </ul> </body> </html> <?php foreach ($music as $key=>$value){ echo $key+1 . ';' .$value .'<br>'; }; ?> <?php echo '<ul>'; foreach ($music as $key=>$value) { echo '<li><a href="">' . ($key+1) . ': ' . $value . '</a></li>'; } echo '</ul>'; ?> <hr> <ul> <?php foreach ($music as $key=>$value) { ?> <li><a href=""><?php echo ($key+1) . ':' . $value; ?></a></li> <?php } ?> </ul> <hr> <?php foreach ($music as $movie): ?> <li><a href=""><?php echo $movie; ?></a></li> <?php endforeach;?>
点击 "运行实例" 按钮查看在线实例
2.get请求:
⑴请求参数以键值对的方式,附加到url地址上,称为查询字符串,用?号与当前脚本分隔
⑵url格式: index.php?name=peter&age=30
⑶受url长度限制, GET方式传递的数据也是有限制的
⑷服务器端脚本使用预定义变量数组 $_GET 进行接收
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title> get </title> </head> <body> <form action="" method="get"> <p> <label for="email">邮箱</label> <input type="text" 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 $_GET['email']; //if (isset($_GET['email'])){ // echo $_GET['email']; //}else { // echo $_GET['']; //} echo isset($_GET['email']) ? $_GET['email']:''; echo '<pre>'; print_r($_GET); ?>
点击 "运行实例" 按钮查看在线实例
3.post请求:
(1)请求参数放在header请求头中发送, url地址看不到请求参数,适合敏感信息
(2)通常是通过表单提交并, 用来更新服务器上的信息
(3)适合发送大量的数据到服务器端, 长度受到配置文件限制,但比GET要大得多
(4)服务器端脚本使用预定义变量数
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title> POST </title> </head> <body> <form action="" method="post"> <p> <label for="email">邮箱</label> <input type="text" 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 isset($_POST['email']) ? $_POST['email']:''; echo '<pre>'; print_r($_POST) ?>
点击 "运行实例" 按钮查看在线实例