一、PHP条件判断
1. 三元运算符 ? :
<?php
$money = 100;
echo $money >= 100 ? '买糖' : '不买糖';
?>
2. if
<?php
$money = 150;
if ($money >= 100){
echo '出去耍';
}
?>
3. if else
<?php
$money = 10;
if ($money >= 100){
echo '出去耍';
}else{
echo '还是在家睡觉吧';
}
?>
4. if elseif else
<?php
$score = 76;
if ($score == 100){
echo '满分';
}elseif ($score >= 80){
echo '优秀';
}elseif ($score >= 70){
echo '良好';
}elseif ($score >= 60){
echo '及格';
}else{
echo '不及格';
}
?>
5. switch case default
<?php
$score = 43;
switch ($score){
case 100:
echo '满分';
break;
case $score >= 90:
echo '优秀';
break;
case $score >= 70:
echo '良好';
break;
case $score >= 60:
echo '及格';
break;
default:
echo '不及格';
break;
}
?>
二、PHP循环
1. while
<?php
$i = 0;
while ($i < 10){
echo $i . '<hr>';
$i++;
}
?>
2. do while
<?php
$i = 1;
do{
echo $i;
echo '<hr>';
$i++;
}while($i<1);
?>
3. for
<?php
for ($i=0;$i<10;$i++) {
echo $i;
echo '<hr>';
}
?>
三、跳出流程语句
1. continue
<?php
for ($i=1;$i<10;$i++) {
if($i == 4){
continue;
}
echo $i;
echo '<hr>';
}
?>
2. break
<?php
for ($i=1;$i<10;$i++) {
if($i == 4){
break;
}
echo $i;
echo '<hr>';
}
?>
四、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="">
<br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" value="">
<br>
<button>登录</button>
</form>
</body>
</html>
<?php
print_r($_GET);
echo '<br>';
echo $_GET['email'];
echo '<br>';
if (isset($_GET['password'])) {
echo $_GET['password'];
}else{
$_GET['password'] = '';
}
echo '<br>';
echo isset($_GET['email']) ? $_GET['email'] : '';
echo '<pre>';
print_r($_GET);
?>
五、综合练习
header.php
<?php
$system = [
'name' => '影视大全',
'copy' => '猪小明'
];
$cates = [
[
'cate_id' => 1,
'name' => 'rm',
'alias' => '热门'
],
[
'cate_id' => 2,
'name' => 'zx',
'alias' => '最新'
],
[
'cate_id' => 3,
'name' => 'dbgf',
'alias' => '豆瓣高分'
],
[
'cate_id' => 4,
'name' => 'lmjp',
'alias' => '冷门佳片'
],
[
'cate_id' => 5,
'name' => 'hy',
'alias' => '华语'
],
[
'cate_id' => 6,
'name' => 'om',
'alias' => '欧美'
],
[
'cate_id' => 7,
'name' => 'hg',
'alias' => '韩国'
],
[
'cate_id' => 8,
'name' => 'rb',
'alias' => '日本'
]
];
$movies = [
[
'cate_id' => 1,
'mov_id' => 1,
'name' => '小丑',
'image' => 'rm1.jpg',
'video' => 'rm1.mp4',
'direct' => '托德·菲利普斯',
'write' => '托德·菲利普斯 斯科特·西尔弗',
'star' => '杰昆·菲尼克斯 罗伯特·德尼罗',
'type' => '剧情 惊悚 犯罪',
'area' => '加拿大 美国',
'language' => '英语',
'date' => '2019-10-04',
'length' => '122分钟',
'detail' => '电影《小丑》以同名DC漫画角色为基础,由华纳兄弟影业公司发行,计划于2019年10月4日上映。本片的故事将独立于DCEU之外,故事背景设置在20世纪80年代,讲述了一位生活陷入困境的脱口秀喜剧演员渐渐走向精神的崩溃,在哥谭市开始了疯狂的犯罪生涯,最终成为了蝙蝠侠的宿敌“小丑”的故事。'
],
[
'cate_id' => 1,
'mov_id' => 2,
'name' => '小小的愿望',
'image' => 'rm2.jpg',
'video' => 'rm2.mp4',
'direct' => '田羽生',
'write' => '田羽生 大宽 大广',
'star' => '彭昱畅 王大陆 魏大勋',
'type' => '剧情 喜剧',
'area' => '中国大陆',
'language' => '汉语普通话',
'date' => '2019-09-12',
'length' => '107分钟',
'detail' => '突然患上肌肉萎缩症的高中生高远(彭昱畅 饰),被医生告知时日不多,决心在临死之前完成一个愿望,那就是成为一个真正的男人!得知这个消息的身边两位好兄弟徐浩(王大陆 饰)和张正阳(魏大勋 饰),为了兄弟最后的愿望,不惜一切奋战到底!'
],
[
'cate_id' => 1,
'mov_id' => 3,
'name' => '最普通的恋爱',
'image' => 'rm3.jpg',
'video' => 'rm1.mp4',
'direct' => '金涵潔',
'write' => '金涵潔',
'star' => ' 金来沅 孔晓振 姜其永',
'type' => '喜剧 爱情',
'area' => '韩国',
'language' => '韩语',
'date' => '2019-10-02',
'length' => '109分钟',
'detail' => 'hanshi.tv电影《最普通的恋爱》将是一部浪漫喜剧,讲述30代男女间的现实爱情,将于今年冬季进行拍摄,预计将于2019年下半年于韩国上映。'
],
[
'cate_id' => 1,
'mov_id' => 4,
'name' => '罗小黑战记',
'image' => 'rm4.jpg',
'video' => 'rm4.mp4',
'direct' => '木头',
'write' => '木头',
'star' => '山新 郝祥海 刘明月',
'type' => '动作 动画 奇幻',
'area' => '中国大陆',
'language' => '汉语普通话',
'date' => '2019-09-07',
'length' => '101分钟',
'detail' => '在熙攘的人类世界里,很多妖精隐匿其中,他们与人类相安无事地生活着。猫妖罗小黑因为家园被破坏,开始了它的流浪之旅。这场旅途中惺惺相惜的妖精同类与和谐包容的人类伙伴相继出现,让小黑陷入了两难抉择,究竟何处才是真正的归属?'
],
// 省略部分
];
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo $system['name'];?></title>
<link rel="stylesheet" href="static/css/style.css">
</head>
<body>
<header>
<div class="nav">
<a href="index.php">首页</a>
<?php
foreach ($cates as $cate) {
echo "<a href='list.php?cate_id=" . $cate['cate_id'] . "'>{$cate['alias']}</a>";
}
?>
</div>
</header>
<main>
footer.php
<?php
$system = [
'name' => '影视大全',
'copy' => '猪小明'
];
?>
</main>
<footer>
<p><?php echo $system['copy'];?> © 版权所有</p>
</footer>
</body>
</html>
index.php
<?php
include __DIR__ . '/header.php';
foreach ($cates as $cate) {
echo "<article><h3>{$cate['alias']}</h3><ul>";
$i = 0;
foreach ($movies as $movie) {
if ($movie['cate_id'] === $cate['cate_id']) {
$i++;
echo "<li><a href='detail.php?mov_id=" . $movie['mov_id'] . "'><img src='static/images/" . $movie['image'] . "' alt=''><span>{$movie['name']}<i>8.9</i></span></a></li>";
if ($i == 5) {
echo '</ul><ul>';
}
}
}
echo '</ul></article>';
}
include __DIR__ . '/footer.php';
list.php
<?php
include __DIR__ . '/header.php';
$cate_id = intval($_GET['cate_id']);
foreach ($cates as $cate) {
if ($cate_id === $cate['cate_id']) {
echo "<article><h3>{$cate['alias']}</h3><ul>";
$i = 0;
foreach ($movies as $movie) {
if ($movie['cate_id'] === $cate['cate_id']) {
$i++;
echo "<li><a href='detail.php?mov_id=" . $movie['mov_id'] . "'><img src='static/images/" . $movie['image'] . "' alt=''><span>{$movie['name']}<i>8.9</i></span></a></li>";
if ($i == 5) {
echo '</ul><ul>';
}
}
}
echo '</ul></article>';
}
}
include __DIR__ . '/footer.php';
detail.php
<?php
include __DIR__ . '/header.php';
$mov_id = intval($_GET['mov_id']);
foreach ($movies as $movie) {
if ($mov_id === $movie['mov_id']){
echo "<h2>{$movie['name']}</h2>";
echo '<div class="video">';
echo '<video src="static/video/' . $movie['video'] . '" autoplay="autoplay" controls="controls" width="800"></video><ul>';
echo '<li><span>导演:</span><a href="">' . $movie['direct'] . '</a></li>';
echo '<li><span>编剧:</span><a href="">' . $movie['write'] . '</a></li>';
echo '<li><span>主演:</span><a href="">' . $movie['star'] . '</a></li>';
echo '<li><span>类型:' . $movie['type'] . '</span></li>';
echo '<li><span>地区:' . $movie['area'] . '</span></li>';
echo '<li><span>语言:' . $movie['language'] . '</span></li>';
echo '<li><span>上映时间:' . $movie['date'] . '</span></li>';
echo '<li><span>时长:' . $movie['length'] . '</span></li></ul></div><div class="detail">';
echo "<h2>{$movie['name']}的剧情简介 · · · · · ·</h2><p>{$movie['detail']}</p></div>";
}
}
include __DIR__ . '/footer.php';
六、总结
学会了php的判断和循环,还掌握了GET传值。