Php üçlü beyanı

3 Cevap php

Merhaba ben son zamanlarda ben kod aşağıdaki satırı genelinde geldi benim kendi sayfa numaralarını inşa böylece onların kod anlamaya çalışırken, bir alışveriş sepeti paginator sınıfı üzerinde bakıyordu. Bu üçlü açıklama andırıyor ama ben daha önce hiç görmediğiniz bir şekilde yazılmıştır. Ben google istiyorum ama google ne bilemeyiz. Birisi bu o yüzden ben bunun için bir arama yapmak ve daha fazla bilgi edinebilirsiniz ne denir nasıl çalıştığını ve ne olduğunu bana söyleyebilir misiniz.

    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>';

Just let me know if this is enough code to go on Thanks Andrew

3 Cevap

Bu koşullu operatörü denir ve bu bunun bir istismar olarak düşünün. Koşullu operatörleri kod okunabilirliği etkileyen olmadan tek bir deyimde kısa if-else yapıları azaltılmasında yararlı olabilir.

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

: Verilen kodu olarak yazılmış olabilir

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>';

Ve eşdeğerdir:

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>';

Nice ... (bazı birleştirme ile birlikte onlara çok iyi, 3,), sadece normal bir koşullu operatörüdür.

Eğer onu yeniden biçimlendirmek, bu biraz daha net olur:

$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;
expression ? runs if true : runs if false;

Daha burada

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

Senin durumunda:

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

$ Çıkış değişkeni boş değilse aşağıdaki'' dize döndürülür, aksi takdirde boş dönmek olduğunu.

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

Aynı kodunuzda kullanılan diğer üçüncül operatörleri ile durumdur.