Intro:The if statement in php

来源:互联网 发布:布柳赫尔知乎 编辑:程序博客网 时间:2024/05/19 23:00
Key skills in this section
if
if-else == ternary operator
if-elseif-else

Try and learn in real-world test
------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">
    <head>
        <title>if.php</title>
    </head>
<body>
    <h2>introphp-if<br />
    odd/even number test</h2>
    <?php
    //if form not yet submitted
    //display form
    if(!isset($_POST['sumbit'])){
    ?>
    <form method="post" action="if.php">
        Enter value:<br />
        <input type="text" name="num" size="3" />
        <br />
        <input type="submit" name="sumbit" value="submit" />
    </form>
    <?php
    //if form sumbitted
    //process form input
    }else{
    //retrieve number from POST submission
    $num=$_POST['num'];
    //test value for even-ness
    //display appropriate message
    if(($num%2)==0){
        echo 'You entered '.$num.',which is an even number.';
    }else{
        echo 'You entered '.$num.',which is an odd number.';
        }
    }
    ?>
</body>

---------------------------------------------------------------------------------------
This program consists of two sections:the first half generates a web form for the user to enter a value,while the second half checks whether the valus is odd or even and prints an appropriate message.In most cases,these two sections would be in separate files;they've been combined into a single PHP script by the magic of a conditional statement.
How does it work?Well,when the web form is sumbmitted,the $_POST variable will contain an entry for the <input type='sumbit'...>element.A conditional test then checks for the presence or absence of this variable:if absent,the program "knows" that the web form has not been submitted and it then proceeds test the input value.
Testing the input value for evenness is handled by a second if-else conditional statement.Here,the conditional expression consists of dividinng the input value by 2 and testing if the remainder is zero.If this test returns true,one message is printed;else,another message is printed.
Writing more complex conditional statements
The if-else statement lets you define actions for two eventualities:a true condition and a false condition.In reality,however,your program may have more than just these two simple outcomes to contend with.For these situations,PHP offers two constructs that allow the programmer to account for multiple possibilities:the if-elseif-else statement and the switch-case statement.
The if-elseif-else statement
The if-elseif-else statement lets you chain together multiple if-else statements,thus allowing the programmer to define actions for more than just two possible outcomes.Consider the following example,which illustrates its use:
---------------------------------------------------------------------------------
<?php
//handle multiple possibilities
//define a different message for each day
$today="Tuesday";
if($today=='Monday'){
echo 'Monday/'s child is fair of face.';
}elseif($today=='Tuseday'){
echo 'Tuesday/'s child is full of grace.';
}elseif($today=='wednesday'){
echo 'Webnesday/'s child is full of woe.';
}elseif($today=='Thursday'){
echo 'Thursday/'s child has far to go.';
}elseif($today=='Friday'){
echo 'Friday/'s child is loving and giving.';
}elseif($today=='Saturday'){
echo 'Saturday/'s child works hard for a living.';
}else{
echo 'No information available for that day';
}
?>
---------------------------------------------------------------------------------------
Here,the program will output a different message for each day of the week(as set in the $today variable).Notice also the final else branch:this is a "catch-all" branch,which will be triggered if none of the previous conditional statements evaluate to true,and it's handy way to account for situations that you can't foresee.
There's one important thing to remember about the if-elseif-else construst:as soon as one of the conditional statements evaluates to true,PHP will execute the corresponding code,skip the remaining conditional tests,and jump straight to the lines following the entire if-elseif-else block.So,even if more than one of the conditional tests evaluates to true,PHP will only execute the code corresponding to the first true test.
原创粉丝点击