-1

有人写了这个“令人惊叹”的简短概括声明。我确信它甚至不应该这样做,但我想剖析它以便其他人(我自己也是)能够通过将它分解为 if 和 else 语句来很好地理解它。我想知道它应该在哪里分解声明。这 ?和 : 位置真的让我很困惑,让我无处可去!(即有?一个接一个?怎么可能?

$cust->is_company() ? 'TekCustomer__'.($cust->getID() ? $cust->getID() : 'new'.($next_new_id-1)) : 'Person__'.($cust->people[0]->getID() ? $cust->people[0]->getID() : 'new'.$next_new_id++) 
4

6 回答 6

1

我强烈建议不要使用嵌套三元运算符。正如您发现的那样,它们远非可读。在这里转换为正确的 if 语句肯定会使其更具可读性。我们可以先对其进行一些格式化以帮助分解它:

$cust->is_company() ? 
    'TekCustomer__'.($cust->getID() ? $cust->getID() : 'new'.($next_new_id-1)) :
     'Person__'.($cust->people[0]->getID() ? $cust->people[0]->getID() : 'new'.$next_new_id++)

啊哈!它实际上并不像最初看起来那样可怕(尤其是由于 PHP 在嵌套三元组上的奇怪行为*)。

首先,它检查$cust->is_company()并做两件事之一。这给了我们:

if($cust->is_company()){
    'TekCustomer__'.($cust->getID() ? $cust->getID() : 'new'.($next_new_id-1));
}else{
    'Person__'.($cust->people[0]->getID() ? $cust->people[0]->getID() : 'new'.$next_new_id++)
}

请注意,通常您会对这些值做一些事情。这将取决于表达式最初是如何使用的。

我将把内部三元作为练习留给读者!

*PHP 从左到右而不是从右到左解析嵌套的三元运算符。见http://php.net/manual/en/language.operators.comparison.php

于 2014-10-21T13:59:19.053 回答
1

你可能会发现它更具可读性:

$cust->is_company() ?
  'TekCustomer__' . ( $cust->getID() ? $cust->getID() : 'new'.($next_new_id-1) )
:
  'Person__' . ( $cust->people[0]->getID() ? $cust->people[0]->getID() : 'new'.$next_new_id++)

基本上它检查它是否是一家公司

  • 是:如果存在,它会TekCustomer__使用当前客户ID构建一个字符串。如果不是,它似乎会生成一个新的 ID。
  • 否:如果存在,它会使用客户人员Person__的当前 ID构建一个字符串。如果不是,它似乎会生成一个新的 ID。
于 2014-10-21T13:58:04.693 回答
1

这就是它应该如何扩展的方式。它被称为三元运算符。

$statement ? value_if_true : value_if_false.

if($cust->is_company()) 
{
    $string = 'TekCustomer__';

    if($cust->getId()) 
    {
        $string .= $cust->getID();  
    } 
    else 
    {
        $string .= 'new'.($next_new_id-1));
    }
} 
else 
{
    $string = 'Person__';

    if($cust->people[0]->getID())
    {
        $string .= $cust->people[0]->getID();
    } 
    else 
    {
        $string .= 'new'.$next_new_id++;
    }
}
于 2014-10-21T13:58:45.223 回答
0

看起来这是赋值右侧的语句。因此,我们需要一个变量来构建字符串,然后用括号将其分解:

$tmp = '';
if ($cust->is_company()) { // first '?'
    $tmp .= 'TekCustomer__';
    if ($cust->getID()) { // second '?'
        $tmp .= $cust->getID();
    } else {
        $tmp .= 'new' . ($next_new_id - 1);
    } 
} else { 
    $tmp .= 'Person__'; 
    if ($cust->people[0]->getID()) { // third '?'
        $tmp .= $cust->people[0]->getID();
    } else { 
        $tmp .= 'new' . $next_new_id++;
    }
}
于 2014-10-21T13:58:56.777 回答
0

I have no idea what each variable holds but this is a way to deconstruct it.

if($cust->is_company())
{
    echo 'TekCustomer__';
    if($cust->getID())
    {
        echo $cust->getID();
    }else
    {
        echo 'new'.$next_new_id-1;
    }
}else
{
    echo 'Person__';
    if($cust->people[0]->getID())
    {
        echo $cust->people[0]->getID();
    }else
    {
        echo 'new'.$next_new_id++;
    }
}
于 2014-10-21T13:57:57.313 回答
0
if ($cust->is_company())
    if ($cust->getID())
        return 'TekCustomer__' . $cust->getID();
    else
        return 'TekCustomer__new'.($next_new_id-1);
else
   if ($cust->people[0]->getID())
        return 'Person__' . $cust->people[0]->getID();
   else 
        return 'Person__new'.$next_new_id++);
于 2014-10-21T13:58:20.203 回答