php if else程序,使用第一个“ if”语句并忽略其余语句

时间:2019-09-14 23:49:45

标签: php if-statement

我正在尝试获取一个要求输入姓名和年龄并提供输出信息的php程序

我知道了姓名和年龄,但是else如果该节不起作用,即使我适当地改变了年龄,其余的也只会被忽略,如果剩下的话。

程序没有给出明显的错误,它只是未按预期运行

echo 'your name is: '.$_GET["name"].'<br>';
echo "<br>";
echo 'your age is: '.$_GET["age"].'<br>';

if(isset($_Get["age"]) <=20)
{
echo "you are under the drinking age<br>";
}



elseif(isset($_Get["age"]) >=21 OR isset($_Get["age"]) <=35)
{
echo "you are a young man";
}

elseif(isset($_Get["age"]) >=36 OR isset($_Get["age"]) <=60)
{
echo "you are a middle aged man";
} 

elseif(isset($_Get["age"]) >=61 OR isset($_Get["age"]) <=100)
{
echo "you are asenior citizen";
} 

elseif(isset($_Get["age"]) >=100 )
{
echo "WOW your really old!!";
}

输出: 你的名字是:亚当

您的年龄是:42 你还不到饮酒年龄

2 个答案:

答案 0 :(得分:0)

您只需要根据需要修改条件即可。

根据您的代码和说明,我认为您想要这样:

HTML代码:

<form method="get">
    <input type="text" name="name">
    <input type="number" name="age">
    <input type="submit" name="submit">
</form>

PHP代码:

<?php
if (isset($_GET['submit'])) { // condition is run only when the form is submited
    $name = $_GET["name"]; // store name in variable
    $age = $_GET["age"]; // store age in variable

    echo 'your name is: '.$name.'<br>';
    echo "<br>";
    echo 'your age is: '.$age.'<br>';

    if($age <=20) { // 20 or less then 20
        echo "you are under the drinking age<br>";
    } else if($age >=21 && $age <=35) { // 21 to 35
        echo "you are a young man";
    } else if($age >=36 && $age <=60) { // 36 to 60
        echo "you are a middle aged man";
    } else if($age >=61 && $age <=99) { // 61 to 99
        echo "you are asenior citizen";
    } else if($age >=100 ) { // Up to 100
        echo "WOW your really old!!";
    }
}
?>

请检查此代码,并尝试了解我的操作,并让我知道您是否有任何疑问。

答案 1 :(得分:-1)

使用filter_var检查号码是否在所需范围内

var pipeline = await initializePipeline();
var obs = Observable.FromEvent(
            callback => pipeline.Start(callback), 
            _ => pipeline.Stop());
obs.Where(Stream.Pose) // get only Poses
   .Select(f => f.As<PoseFrame>().PoseData) // select only what we need
   .Where(pose_data => pose_data.translation.x != -300) // filter out?
   .Sample(TimeSpan.FromMilliseconds(100)) // throttle the UI
   .Buffer(2, 1) // gives you last and current items
   .ObserveOn(this) // dispatch on the UI
   .Subscribe(data => {var last=data[0]; var current=data[1]; /* update labels*/});
await obs.GetAwaiter();