开发者

PHP conditional return statement?

Can someone tell me what the condition is in this php statement?

return $node->type == 'article' ? mymodule_page_article($node) : mymodule_page_story($node);

I'm sorry if this is not the place to ask such a simple question but I'm finding it diffi开发者_如何学运维cult to look up specific code structure (especially when I don't know the name of it).


This is a ternary operator.

It's equivalent to

if( $node->type == 'article' ) {
    return mymodule_page_article($node);
} else {
    return mymodule_page_story($node);
}

What it does is: if the stuff before the ? is true, return the result of the expression in the first clause (the stuff between ? and :). If it's false, then it returns the result of the second clause (the stuff after the :).


This is the ternary operator ?: and can be expanded as follows:

if ($node->type == 'article') {
  return mymodule_page_article($node);
} else {
  return mymodule_page_story($node);
}


This is equivalent to:

if($node->type == 'article')
{
     return mymodule_page_article($node);
}
else
{
     return mymodule_page_story($node);
}

This is called the ternary operator. See the section on it here for more information: http://www.php.net/operators.comparison


this is a ternary expression.

the condition is $node->type == 'article' and if it's true it does mymodule_page_article($node) else mymodule_page_story($node)


If type of node is equal to 'article' do mymodule_page_article($node), if it isn't equal then do mymodule_page_story($node)


Use Ternary operator

return isset($node->type == 'article') ? mymodule_page_article($node) : mymodule_page_story($node)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜