Blogger Information
Blog 34
fans 0
comment 0
visits 22396
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP流程控制—php九期
曾龙宇
Original
595 people have browsed it

一、流程控制

①、三元运算符: 判断式?'一':'二'

②、if(条件){语句块}else{语句块}

③、if(条件){语句块}else if(条件){语句块}else{语句块}

④、switch(条件){case 1:语句;break;}

<?php
//三元运算符
$number = 1000;
echo $number>1200?'买买买':'伤不起啊';

//if-else
$num = 50;
if ($num>50){
    echo '大于50';
}else{
    echo '小于等于50';
}

// if elseif else
$num = 60;
if ($num>=100){
    echo '可以出去玩';
}elseif ($num>=80){
    echo '可以看电视';
}elseif ($num>=60){
    echo '不挨打就好了,还想出去玩';
}else{
    echo '狠狠揍一顿';
}

//switch
$score = 100;
switch ($score/10){
    case 10:
        echo '满分,干得漂亮';
        break;
    case 9:
        echo '很优秀';
        break;
    case 8:
        echo '良好';
        break;
    case 7:
    case 6:
        echo '合格';
        break;
    default:
        echo '去复习吧';
        break;
}

二、计数循环

①、while{语句块}

②、do{语句块}();

③、for

<?php

//while
$int = 1;
while($int<10){
    echo $int.' ';
    $int++;
}

//do-while
$int = 1;
do{
    echo $int;
    $int++;
}while($int<1);

// for
for ($int=1;$int<10;$int++){
    echo $int.' ';
}

break:结束循环

continue:结束当前循环,进入下次循环


三、get传值

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>get传值</title>
</head>
<body>
<form action="" method="get">
    <div>
        <label for="username">账号</label>
        <input type="text" name="name" id="username" value="">
    </div>
    <div>
        <label for="pwd">密码</label>
        <input type="password" name="pwd" id="pwd" value="">
    </div>
    <div>
        <button>登录</button>
    </div>
</form>
</body>
</html>
<?php
print_r($_GET);
echo '<br>';
echo $_GET['name'].'<br>';
echo $_GET['pwd'];
?>


blob.png



四、模板分离案例:

index.php

<?php require 'header.php' ;?>

<?php foreach ($cates as $cate){?>
<h2><?php echo $cate['alias'];?></h2>
<ol>

    <?php foreach($movies as $movie){
        if($movie['cate_id'] == $cate['cate_id']){
    ?>
            <li><a href="detail.php?cate=<?php echo $movie['cate_id']?>"><?php echo $movie['name'];?></a></li>
    <?php } }?>
</ol>
<?php }?>
<?php require 'footer.php';?>

list.php

<?php include 'header.php' ?>

<?php
$cate_id = intval($_GET['cate_id']);
foreach ($cates as $cate){
    if ($cate['cate_id'] == $cate_id){
?>
<h2><?php echo $cate['alias']?></h2>
<ol>
    <?php
    foreach ($movies as $movie){
        if ($movie['cate_id'] == $cate_id){
    ?>
    <li><a href="detail.php?mov_id=<?php echo $movie['mov_id']?>"><?php echo $movie['name']?></a></li>
        <?php } }?>

</ol>
<?php } }?>
<?php include 'footer.php'?>

detail.php

<?php include 'header.php'?>

<?php
$mov_id = intval($_GET['mov_id']);
foreach ($movies as $movie){
    if ($movie['mov_id'] == $mov_id){
?>
<h3><?php echo $movie['name']?></h3>
<img src="images/<?php echo $movie['image']?>" alt="" width="300">
<p style='text-indent: 2em'><?php echo $movie['detail']?></p>
<?php } }?>

<?php include 'footer.php'?>


Correcting teacher:查无此人查无此人

Correction status:qualified

Teacher's comments:完成的不错
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post