Prince
Prince

Reputation: 1192

Simplify if else condition with ternary operator

I am having the following code.

if ($auth->user()){
    echo $session->getDetails('username');
} else {
    echo 'world';
}

I found out that I can make it simple by using the ternary operator, so I modified the code above to get something like this:

($auth->user() ? $session->getDetails('username') : 'world');

The problem I am facing is the portion $session->getDetails('username') is not displayed. Kindly help me solve it

Upvotes: 1

Views: 132

Answers (1)

Ibrahim Lawal
Ibrahim Lawal

Reputation: 1228

Add an echo or assign a variable, so:

echo ($auth->user() ? $session->getDetails('username') : 'world');

or

$username = $auth->user() ? $session->getDetails('username') : 'world';
echo $username;

Upvotes: 4

Related Questions