Colon operator in PHP

孤人 提交于 2019-12-04 07:24:44

Reggie,

colons in if/else statements in PHP : it's NOT about replacing braces but a PAIR of braces.

Example :

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

would become

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

OR (since it's just one statement and not a block of statements)

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

Reference : Alternative Syntax for Control Structures


As for this specific piece of code :

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

it translates to

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

Hint : The elseif statement does NOT include the other elseif statements. (like elseif ($a) { elseif($b) {} })

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!