带有年龄问候语的 Isset 函数 PHP

Isset function with age greeting PHP

我开始学习 PHP 而且我一直坚持这个练习... 这是我们追求的用户体验: 首先,用户会看到一个询问他们年龄的表格:

Please type your age : __

当他们提交表单时,页面会显示正确的消息:

if age is less than 12 years old, display "Hello kiddo!"
if age is between 12 and 18 years old, display "Hello Teenager !"
if age is between 18 and 115 years old, display Hello Adult !"
if age is beyond 115 years old, display "Wow! Still alive ? Are you a robot, like me ? Can I hug you ?"

将表单和处理脚本放在同一个文件中。使用 GET 方法。

这是一个先机。


// 3. "Different greetings according to age" Exercise

if (isset($_GET['age'])){
    // Form processing

}
// Form (incomplete)
?>
<form method="get" action="">
    <label for="age">...</label>
    <input type="" name="age">
    <input type="submit" name="submit" value="Greet me now">
</form>

我真的不知道如何在这个函数中使用isset :-( 谁能给我一点推动力?欢迎!谢谢!

下面是isset()的解释:

Determines if a variable is declared and is different than null.

因此在您的情况下,您只是检查是否已设置 $_GET['age']。如果是,您将根据输入打印一条消息,否则将跳过 if 语句:

<?php

    if (isset($_GET['age']) && ctype_digit($_GET['age'])) {
        $age = $_GET['age'];

        if($age > 0 && $age <= 12) {
            echo 'Hello kiddo!';
        } else if($age > 12 && $age < 18) {
            echo 'Hello Teenager !';
        } else if($age >= 18 && $age <= 115) {
            echo 'Hello Adult !';
        } else if($age > 115) {
            echo 'Wow! Still alive ? Are you a robot, like me ? Can I hug you ?';
        }
    }
?>

<form method="get" action="">
    <label for="age">Please type your age: </label>
    <input type="text" id="age" name="age" value="" />
    <input type="submit" name="submit" value="Greet me now">
</form>

我还向您的输入元素添加了一个 type="text" 属性以及一个 id<label> 使用它的 for 属性来引用正确的元素.

注意:上面的ctype_digit()方法检查变量是否有数字字符。这是可选的,可以删除,但它有助于确保传递了正确的值类型。

我强烈建议您查看相关 documentation 以及其中的示例。只是为了给你一点推:

  • isset (在您的情况下)用于检查年龄是否出现在 $_GET 数组中。这转化为:检查是否提交了具有 age
  • 特定值的表单
  • 您的工作(如果以上为真)是将 $_GET['age'] 的值与您的任何规则进行比较,并显示所需内容作为消息。
if (isset($_GET['age']) && is_int($_GET['age'])){
    // Form processing
    // Form processing
    $providedAge = $_GET['age'];
    
    switch ($providedAge) {
        case $providedAge <= 12:
            echo 'Hello kiddo!';
            break;
        case $providedAge > 12 && $providedAge < 18:
            echo 'Hello Teenager !';
            break;
        case $providedAge >= 18 && $providedAge <= 115:
            echo 'Hello Adult !';
            break;
        case $providedAge > 115:
            echo 'Wow! Still alive ? Are you a robot, like me ? Can I hug you ?';
            break;
        default:
            echo 'Ops!';
    }
}
// Form (incomplete)
?>
<form method="get" action="">
    <label for="age">...</label>
    <input type="" name="age">
    <input type="submit" name="submit" value="Greet me now">
</form>

如果您不熟悉 switch 构造,请也检查一下

一种略有不同的方法,可以很容易地通过仅修改源 $greetings 数组来扩展 - 遍历各种问候语并在给定年龄超过该问候语的最大值时停止。

<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title></title>
    </head>
    <body>
        <form method='get'>
            <label for='age'>...Age: <input type='' name='age'></label>
            <input type='submit' name='submit' value='Greet me now'>
        </form>

        <?php


            
            /* Only proceed if there is a GET variabe `age` set... otherwise do nothing */
            if( isset( $_GET['age'] ) ){
                
                /* Set the max age for each greeting as the key */
                $greetings=array(
                    0   =>  'Hello glint in your father\'s eye',                            #if below zero
                    12  =>  'Hello kiddo!',                                                 #if age is less than 12 years old
                    18  =>  'Hello Teenager!',                                              #if age is between 12 and 18 years old
                    115 =>  'Hello Adult!',                                                 #if age is between 18 and 115 years old
                    116 =>  'Wow! Still alive? Are you a robot, like me? Can I hug you?'    #if age is beyond 115 years old
                );
                
                /* filter the Querystring variable and ensure we are dealing with a numeric value only */
                $age=filter_var(
                    filter_input( INPUT_GET, 'age', FILTER_SANITIZE_NUMBER_INT ), 
                    FILTER_VALIDATE_INT
                );
                
                if( is_numeric( $age ) ){
                    /* loop through the greetings and stop when the given age exceeds the value for the key */
                    foreach( $greetings as $i => $greeting ){
                        if( $age <= $i )break;
                    }
                    echo $greeting;
                }
            }
        ?>
    </body>
</html>