[英]PHP using false to break out of if/else shorthand
使用PHP if / else速記並想知道這是否可以接受:
$pages = get_children(array(
'post_parent' => 49,
'post_type' => 'page',
'post_status' => 'publish',
));
$html = '';
$first = true;
$first_id = null;
foreach ($pages as $page) :
$html .= '<li class=""><a href="" class="js-load-sub-nav' . ( $first ? " is-active" : "" ) . '" data-id="' . $page->ID . '" data-action="af_contact_nav_child_pages">' . $page->post_title . '</a></li>';
( $first ? $first_id = $page->ID : false );
$first = false;
endforeach;
有問題的代碼段:
( $first ? $first_id = $page->ID : false );
似乎工作正常??
該技術是眾所周知的-稱為三元運算符。 我不覺得這很丑:)
如果切換邏輯,甚至可以忽略false
:
!$first ?: $first_id = $page->ID;
請注意,僅后面的部分?
可以保留為空。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.