第一例,文字加粗显示
这里使用heredoc或nowdoc可以更好的输出html代码块
header('content-type:text/html;charset=utf-8');
$str = <<<'B'
<b>文字加粗显示</b>
B;
echo $str;
第二例,php动态输出javascript脚本
header('content-type:text/html;charset=utf-8');
$str = <<<EOF
<script>alert('动态网页之美')</script>
EOF;
echo $str;
第三例 ,使用可变变量输出hello php;
header('content-type:text/html;charset=utf-8');
$a = 'name';
$$a = 'hello php';
echo($name);
第四例,输出隔行换色的列表
header('content-type:text/html;charset=utf-8');
echo "<ul>";
for ($i = 1;$i<6;$i++){
if ($i%2==0) {
$color = 'red';
}else{
$color = 'yellow';
}
echo "<li style='background-color:$color;width: 300px;'>{$i}</li>";
}
echo "</ul>";
第五例,简单的用户登录验证
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/9/19 0019
* Time: 3:26
*/
header('content-type:text/html;charset=utf-8');
if (isset($_POST)){
if ($_POST['name']=='admin'&&$_POST['password']=='123456'){
echo '登陆成功';
}else{
echo '登陆失败';
}
}
?>
<!doctype html>
<html lang="en">
<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>登陆</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<label for="name">用户名:</label>
<input type="text" name="name" id="name"><br>
<label for="pwd">密 码:</label>
<input type="password" name="password" id="pwd"><br>
<input type="submit" value="登陆">
<input type="reset">
</form>
</body>
</html>