PHP中嵌套的If/Else


Nested If / Else in PHP

我对任何编码都很陌生,所以如果这看起来很基本,请耐心等待。

我正在尝试在一封带有变量$fname的电子邮件中分配适当的问候语。名称值源自的数据是一个简单的单个字符串。因此,格式上可能有多种变化。

在这个特定的应用程序中,细节大多写为:

-约翰·史密斯先生
-Smith先生
-约翰·史密斯
-约翰·

因此,在将名称分配给变量之前,我需要评估用于输入名称的方法。

我已经写了下面的代码,但在线检查会弹出"一个意想不到的‘其他’"。

在我看来一切都很好,我所看到的网上例子似乎也遵循着类似的顺序。

非常感谢您的帮助!

谢谢,

罗伯。

OK-使用正确的、实际测试的工作代码再次编辑。(我昨天很累,错过了一个可能的结果)这绝对适用于我需要的所有排列。希望它能为其他初学者节省我做这么简单的事情所花费的疯狂时间

再次感谢所有帮助我的人,并为我的"新手"身份道歉。

 <?php
    $saltest = explode(" ", $client);
    if (empty($saltest[1])) {  // checks to see if it a single name eg 'John'.
    $fname = $saltest[0];
    } elseif (isset($saltest[1])) { // if it is not a single word, checks to see if starts with a salutation
    switch ($saltest[0]) {
        case 'Mr';
        case 'Mrs';
        case 'Ms';
        case 'Mr.';
        case 'Mrs.';
        case 'Ms.';
        case 'Dr';
        case 'Dr.';
        $withsal = $saltest;    // if it does, allocate the array to variable $withsal.
    break;
        default: $withoutsal = $saltest; // if it doesn't - allocate it to variable $withoutsal.
        break;
    }
    }
    if (isset($withoutsal)) { // if already established that there is no salutation, issue the $fname & $lname variables
    $fname = $withoutsal[0];
    $lname = $withoutsal[1];
    } elseif (isset($withsal) and isset($withsal[2])) { // if it has a salutation and is compised of 3 words - issue $fname & $lname accordingly.
    $fname = $withsal[1];
    $lname = $withsal[2];   
    } elseif (isset($withsal) and isset($withsal[1])) { // if it has a salutation but is only 2 words, put them together to create variable $fname eg. "Mr Smith" and declare the $lname variable as NULL. It is not needed. 
    $fname = implode(" ", $withsal);
    $lname = NULL;  
    }
    echo $fname;
    ?>

在另一个else之后有一个else语句。您需要在它们之间获得一个if语句,或者希望使用elseif。根据您的评论。(// but if there was no salutation and only 2 names were given...)您需要更改为elseif

缺少一些括号

<?php
    // splits the name field into parts
    $names = explode(" ", $client);
    // works out if the name field starts with a salutation
    if ($names[0] == ("Mr" || "Mrs" || "Ms" || "Mr." || "Mrs." || "Ms.")) 
    {
     // decides if it is written as "Salutation, surname"
        if (empty($names[2])) 
        {
            $lname = $names[1];
            $salname = array($names[0], $names[1]);
            $fname = implode(" ", $salname);
    // evaluates that if it starts with a salutation and has 3 names, it must then be "salutation firstname lastname"
        } 
        else 
        {        
            $fname = $names[1];
            $lname = $names[2];
        // but if there was no salutation and only 2 names were given...
        }
    } 
    else 
    {        
        if (empty(names[2])) 
        {
            $fname = $names[0];
            $lname = $names[1];
        // otherwise it must be "first, middle, last"
        } 
        else 
        {        
            $fname = $names[0];
            $lname = $names[3];
        }
    }
?>