将嵌套的PHP if语句转换为单个if语句

时间:2015-03-12 03:50:15

标签: php if-statement nested

我试图弄清楚如何将这个嵌套的if语句转换为单个if语句并且无法让它运行。

这是原始的嵌套if语句:

if (isset($_REQUEST['gender'])) 
    {
        $gender = $_REQUEST['gender'];

        if ($gender == 'M') 
        {
            echo '<p><b>Good day, Sir!</b></p>';
        } 
        elseif ($gender == 'F') 
        {
            echo '<p><b>Good day, Madam!</b></p>';
        } 
        else 
        { // Unacceptable value.
            $gender = NULL;
            echo '<p class="error">Gender should be either "M" or "F"!</p>';
        }
    } 
    else 
    { // $_REQUEST['gender'] is not set.
        $gender = NULL;
        echo '<p class="error">You forgot to select your gender!</p>';
    }

这是我的尝试:

        if (isset($_REQUEST['gender']) && $gender == 'M') 
    {
        $gender = $_REQUEST['gender'];
        echo '<p><b>Good day, Sir!</b></p>';
    } 
    elseif (isset($_REQUEST['gender']) && $gender == 'F') 
    {
        $gender = $_REQUEST['gender'];
        echo '<p><b>Good day, Madam!</b></p>';
    } 
    elseif (isset($_REQUEST['gender']) && $gender != 'M' || (isset($_REQUEST['gender']) && $gender != 'F') 
    { // Unacceptable value.
        $gender = $_REQUEST['gender'];
        $gender = NULL;
        echo '<p class="error">Gender should be either "M" or "F"!</p>';
    }
    else
    { // $_REQUEST['gender'] is not set.
        $gender = NULL;
        echo '<p class="error">You forgot to select your gender!</p>';
    }

我不确定是不是因为$gender = $_REQUEST['gender'];没有执行,但任何帮助都会受到赞赏。

3 个答案:

答案 0 :(得分:2)

在为其分配值之前,您正在检查变量$gender。以下将有所帮助,

$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender'] : "";
if ($gender == 'M') 
{
    echo '<p><b>Good day, Sir!</b></p>';
} 
else if($gender == 'F') 
{
    echo '<p><b>Good day, Madam!</b></p>';
} 
else if (!empty($gender) && ($gender != 'M' || $gender != 'F'))
{   // Unacceptable value.
    echo '<p class="error">Gender should be either "M" or "F"!</p>';
}
else
{ 
   echo '<p class="error">You forgot to select your gender!</p>';
}

答案 1 :(得分:1)

这里的switch语句可能更清晰。

switch ($_REQUEST['gender']) {
    case null:
        echo '<p class="error">You forgot to select your gender!</p>';
        break;
    case 'M':
        echo '<p><b>Good day, Sir!</b></p>';
        break;
    case 'F':
        echo '<p><b>Good day, Madam!</b></p>';
        break;
    default:
        echo '<p class="error">Gender should be either "M" or "F"!</p>';
}

答案 2 :(得分:0)

这是我未经测试的方式。我也先做客户端(javascript)检查。我还假设您的性别字段是文本,如果选择您可以取出strtolower并修剪。

<?php
if (empty($_REQUEST['gender']) || (strtolower(trim($_REQUEST['gender'])) != 'm' && strtolower(trim($_REQUEST['gender'])) != 'f')) {
    //this should probably be checked client side for better usability, but better to check server side that we trust the data
    echo '<p class="error">Please enter a valid Gender</p>';
} else {
    $gender = strtolower(trim($_REQUEST['gender']));
    echo '<p><b>Good day, ';
    if ($gender == 'm') {
        echo 'Sir!</b></p>';
    }  else {
        echo 'Madam!</b></p>';
    }
}