【问题标题】:Php ternary statementphp三元语句
【发布时间】:2010-12-22 08:59:53
【问题描述】:

您好,我最近正在查看一个购物车分页器类,试图理解他们的代码,以便在遇到以下代码行时构建自己的分页器。它类似于三元语句,但以我从未见过的方式编写。我会用谷歌搜索它,但我不知道用谷歌搜索什么。有人可以告诉我它是如何工作的以及它叫什么,这样我就可以搜索它并了解更多信息。

    return ($output ? '<div class="' . $this->style_links . '">' . $output . '</div>' : '') 
. '<div class="' . $this->style_results . '">' . sprintf($this->text, ($total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($total - $limit)) ? $total : ((($page - 1) * $limit) + $limit), $total, $num_pages) . '</div>';

只要让我知道这是否足够继续运行的代码 谢谢 安德鲁

【问题讨论】:

  • 我想知道谁来维护这些被滥用的条件运算符语句。

标签: php conditional-operator


【解决方案1】:
expression ? runs if true : runs if false;

这里有更多

 http://www.johnhok.com/2008/02/23/php-tip-tertiary-operator/

在你的情况下:

$output ? '<div class="' . $this->style_links . '">' . $output . '</div>' : ''

如果 $output 变量不为空,则返回,否则返回空字符串。

<div class="' . $this->style_links . '">' . $output . '</div>'

您的代码中使用的其他三级运算符也是如此。

【讨论】:

  • 好的,所以如果你只是放一个变量,你说的是这个变量是空的。我没有意识到你可以这样使用它们
  • 你的意思是 ($output == '' ) 吗?即输出等于什么?
  • 没有。仅将$output 作为条件就相当于测试它 not 等于无...
【解决方案2】:

很好...它只是一个常规条件运算符(嗯,其中 3 个,以及一些连接)。

如果你重新格式化它,它会变得更清晰:

$output = $output ? '<div class="' . $this->style_links . '">' . $output . '</div>' : '';

$min = $total ? (($page - 1) * $limit) + 1 : 0;
$max = (($page - 1) * $limit) > ($total - $limit) ? $total : ((($page - 1) * $limit) + $limit);

$output .= '<div class="' . $this->style_results . '">'
    . sprintf($this->text, $min, $max, $total, $num_pages)
    . '</div>';

return $output;

【讨论】:

  • 好吧,我不明白第一行。问号是什么意思,我认为三元运算符像这样 $string = (condtion)?do if true:do if false;条件在哪里?还是输出条件,怎么可能是条件?
  • 以前没见过这样用的,是怎么回事
  • 一个简单的变量也是一个表达式。如果变量的值“等于”true,则返回 true,因此它不应该是 0falsearray() 或空字符串...
  • 这就像使用if ()if ($output)if ($output == true) 相同 - 同样$output ? x : y$output == true ? x : y 相同
【解决方案3】:

它被称为条件运算符,我认为这是对它的滥用。在不影响代码可读性的情况下,条件运算符有助于将简短的 if-else 构造简化为一条语句。

if(a == b)
    c = d;
else
    c = e;
//can be written as:
c = a == b ? d : e;

给定的代码可以写成:

return ($output ? 
            '<div class="' . $this->style_links . '">' . $output . '</div>'
         : '') . 
    '<div class="' . $this->style_results . '">' . 
    sprintf($this->text, 
        ($total) ? 
            (($page - 1) * $limit) + 1 
          : 0, 
        ((($page - 1) * $limit) > ($total - $limit)) ? 
            $total 
          : ((($page - 1) * $limit) + $limit), 
        $total, $num_pages) . '</div>';

并且等价于:

if($output)
    $str = '<div class="' . $this->style_links . '">' . $output . '</div>';
else
    $str = '';

$str .= '<div class="' . $this->style_results . '">';

if($total)
    $first = (($page - 1) * $limit) + 1;
else
    $first = 0;

if((($page - 1) * $limit) > ($total - $limit))
    $second = $total;
else
    $second = ((($page - 1) * $limit) + $limit);

$str .= sprintf($this->text, $first, $second, $total, $num_pages);
$str .= '</div>';

【讨论】:

  • 除了在他们的情况下它只是 $a = $b?c:d;我认为这很奇怪,因为我没有意识到你可以在不使用 empty($var) 的情况下测试一个变量是否为空
  • if($output) 计算结果为 true 如果 $output 在之前的某处定义并且不是空字符串。空字符串为false
  • 啊,好吧,这是有道理的。谢谢
  • " 转换为布尔值时,以下值被视为 FALSE: * 布尔值 FALSE 本身 * 整数 0(零) * 浮点数 0.0(零) * 空字符串和字符串 "0 “ * 一个零元素数组 * 一个零成员变量的对象(仅限 PHP 4) * 特殊类型 NULL(包括未设置的变量) * 从空标签创建的 SimpleXML 对象” php.net/manual/en/… 作为可能遇到此问题的人的参考搜索时的问题:)
猜你喜欢
  • 2016-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 2011-12-01
相关资源
最近更新 更多