Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 difficult to look up specific code structure (especially when I don't know the name of it).

like image 943
Nate Avatar asked Dec 12 '22 09:12

Nate


1 Answers

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 :).

like image 166
tskuzzy Avatar answered Dec 27 '22 16:12

tskuzzy