Mike
Mike

Reputation:

Object and Ternary Operator PHP

I wanna make use of the Ternary Operator with an object.

if($msg == 'hello'){
    $o->setHello('hello');
else
    $o->setHello('bye');

How could I do that?

Thanks

Upvotes: 3

Views: 806

Answers (3)

Cade Roux
Cade Roux

Reputation: 89671

How about?

$o->setHello($msg == 'hello' ? 'hello' : 'bye');

Upvotes: 9

1800 INFORMATION
1800 INFORMATION

Reputation: 135295

$o->setHello($msg == 'hello' ? 'hello' : 'bye');

Upvotes: 23

Noah Goodrich
Noah Goodrich

Reputation: 25263

Try this:

($msg == 'hello') ? $o->setHello('hello') : $o->setHello('bye');

Upvotes: 2

Related Questions