2012-04-07 26 views
5

Đây là một phần của mã wordpress và tôi không hiểu nó:hành Colon trong PHP

if  (is_404()   && $template = get_404_template()   ) : 
elseif (is_search()   && $template = get_search_template()  ) : 
elseif (is_tax()   && $template = get_taxonomy_template()  ) : 
elseif (is_front_page()  && $template = get_front_page_template() ) : 
elseif (is_home()   && $template = get_home_template()   ) : 
elseif (is_attachment()  && $template = get_attachment_template() ) : 
    remove_filter('the_content', 'prepend_attachment'); 
elseif (is_single()   && $template = get_single_template()  ) : 
elseif (is_page()   && $template = get_page_template()   ) : 
elseif (is_category()  && $template = get_category_template()  ) : 
elseif (is_tag()   && $template = get_tag_template()   ) : 
elseif (is_author()   && $template = get_author_template()  ) : 
elseif (is_date()   && $template = get_date_template()   ) : 
elseif (is_archive()  && $template = get_archive_template()  ) : 
elseif (is_comments_popup() && $template = get_comments_popup_template()) : 
elseif (is_paged()   && $template = get_paged_template()   ) : 
else : 
    $template = get_index_template(); 
endif; 

Một thư ruột kết có thể thay thế một khung nhọn trong PHP. Vì vậy, nếu tôi thay thế các dấu hai chấm, tôi nhận được điều này:

if  (is_404()   && $template = get_404_template()   ) { 
    elseif (is_search()   && $template = get_search_template()  ) { 
     elseif (is_tax()   && $template = get_taxonomy_template()  ) { 
     ... 
     } 
    } 
} 
else 

Không có ý nghĩa với tôi, vì mỗi elseif bị thiếu mở nếu.

+9

Tôi đã chết một chút ... –

+2

đó là .... khủng khiếp. – dmp

+0

Tôi nghĩ rằng tôi biết những gì nhầm lẫn OP ... –

Trả lời

17

Reggie,

dấu hai chấm trong if/else trong PHP: nó không phải về thay thế niềng răng nhưng một cặp niềng răng.

Ví dụ:

if ($a) : doThis(); 
elseif ($b) : doThat(); 
else : doTheOther(); 
endif; 

sẽ trở thành

if ($a) { doThis(); } 
elseif ($b) { doThat(); } 
else { doTheOther(); } 

HOẶC(vì nó chỉ là một tuyên bố và không phải là một khối lệnh)

if ($a) doThis(); 
elseif($b) doThat(); 
else doTheOther(); 

tham khảo:Alternative Syntax for Control Structures


Đối với mảnh cụ thể mã này:

if  (is_404()   && $template = get_404_template()   ) : 
elseif (is_search()   && $template = get_search_template()  ) : 
elseif (is_tax()   && $template = get_taxonomy_template()  ) : 

nó dịch để

if  (is_404()   && $template = get_404_template()   ) 
    { /* DO NOTHING */ } 
elseif (is_search()   && $template = get_search_template()  ) 
    { /* DO NOTHING */ } 

Gợi ý: Tuyên bố elseif KHÔNG bao gồm các câu lệnh elseif khác. (như elseif ($a) { elseif($b) {} })