Correction status:qualified
Teacher's comments:代码不仅要写正确, 还要记住
PHP知识:
1. <?php ?>
2. 创建变量直接: $value = value; 数组 $arr = ['……','……','……'];
3. foreach() 遍历 例: foreach( $movies as $key => $value){……};
<?php $title = '这是标题'; ?> <?php $array = ['内容1','内容2','内容3'] ;?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title> <?php echo $title; ?> </title> </head> <body> <h2><?php echo $title; ?></h2> <!--foreach() foreach遍历输出html内容--> <?php foreach ($array as $key => $value){ ?> <li><a href="#"> <?php echo $value; ?></a></li> <?php } ?> <hr> <!--foreach()替代语法输出html内容--> <?php foreach ($array as $array): ?> <li><a href=""><?php echo $array; ?></a></li> <?php endforeach; ?> </body> </html>
点击 "运行实例" 按钮查看在线实例
GET/POST处理:
1. echo '<pre>' 格式化输出;
2. print_r ($_GET) ; 打印GET;
3. 获取变量之前用isset()进行判断
if (isset($_GET['email'])) { echo $_GET['email']; //GET改为POST } else { // 给个默认值 $_GET['email'] = ''; //GET改为POST }
简化 三元运算符
echo isset($_GET['email']) ? $_GET['email'] : '' ; //GET改为POST
粘性表单:
<input type="text" name="name名" id="id名" value="<?php echo isset($_GET['email']) ? $_GET['email'] : '' ; ?>"
<?php $title = '这是表单'; ?> <?php $array = ['内容1','内容2','内容3'] ;?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title> <?php echo $title; ?> </title> </head> <body> <form action="" method="get"> <label for="username">账号:</label> <input type="text" name="username" id="username" value="echo isset($_GET['username']) ? $_GET['username'] : ''"> <br> <label for="pwd">密码:</label> <input type="password" name="pwd" id="pwd" value="echo isset($_GET['pwd']) ? $_GET['pwd'] : ''"> <br> <label for="email">邮箱:</label> <input type="email" name="email" id="email" value="echo isset($_GET['email']) ? $_GET['email'] : '';"> <br> <button>登录</button> <?php echo '<pre>'; print_r($_GET); // if (isset($_GET['email'])){ // echo $_GET['email']; // }else{ // $_GET['email'] = ''; // } ?> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例
<?php $title = '这是表单'; ?> <?php $array = ['内容1','内容2','内容3'] ;?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title> <?php echo $title; ?> </title> </head> <body> <form action="" method="post"> <label for="username">账号:</label> <input type="text" name="username" id="username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : ''; ?>"> <br> <label for="pwd">密码:</label> <input type="password" name="pwd" id="pwd" value="<?php echo isset($_POST['pwd']) ? $_POST['pwd'] : '';?>"> <br> <label for="email">邮箱:</label> <input type="email" name="email" id="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '';?>"> <br> <button>登录</button> <?php echo '<pre>'; print_r($_POST); ?> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例