4

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

6 Answers 6

10

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

Sign up to request clarification or add additional context in comments.

Comments

2

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);
}

Comments

1

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

Comments

0

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)

Comments

0

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

Comments

0

Use Ternary operator

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.