I am passing variable to my index view in Yii2 framework. I have the following code:
return $this->render('index', array(
'userresult' => $userresult,
'topresult' => $topresult,
'result' => $result
));
I only need to pass variable $userresult if user is logged in since if user is not logged in, the $userresult variable does not exist. This is what I tried but I can not get the if statement to run:
return $this->render('index', array(
if (!\Yii::$app->user->isGuest) { echo "'userresult' => $userresult"; },
'topresult' => $topresult,
'result' => $result
));
How can this be achieved?