只要输入为“女性”且已婚,则不显示任何内容,但如果输入为“单身女性”,则会显示输出


Nothing appears whenever the input is Female and married but there is an output if the input is Female an single

我是一个PHP程序员,需要帮助。

无论何时输入是Femalemarried,都不会出现任何内容,但如果输入是Femalesingle,则会出现输出。我很难弄清楚。

这是代码:

if($gender =="Female" And $status=="married"){      
    $output.= " <b>  Your Fullname: </b>  Mrs.  " .$fname.    "    "  .$mname.   "   " .$lname.   "</br>" ;
}
else{
    if($gender =="Female" And $status =="single"){      
        $output.= " <b>  Your Fullname: </b> Ms.  " .$fname.    "    "  .$mname.   "   " .$lname.   "</br>" ;
    } else{
        $output.= " <b>  Your Fullname: </b>  Mr. " .$fname.    "    "  .$mname.   "   " .$lname.   "</br>" ;
    }
}

我们需要知道$gender$status的解析结果,才能真正知道你应该得到什么条件。

你可以把它浓缩成一个三元运算符。。。

$output.= " <b>  Your Fullname: </b>  ". $gender == 'Female' ? ($status == 'married' ? 'Mrs.' : 'Ms.') : 'Mr.' ."  " .$fname.    "    "  .$mname.   "   " .$lname.   "</br>" ;

或者对它们的名称进行预处理。。。

$prefix = 'Mr.';
if($gender == 'Female')
{
    if($status == 'married')
    {
        $prefix = 'Mrs.';
    }
    else
    {
        $prefix = 'Ms.';
    }
}
$output.= " <b>  Your Fullname: </b>  " .$prefix. "  " .$fname.    "    "  .$mname.   "   " .$lname.   "</br>" ;

虽然and在技术上可以充当&&,但您可能需要遵守惯例。&&将是最常见的语法。我最初的怀疑是,$status实际上从未=="已婚"。