i passing variable index
view in yii2 framework. have following code:
return $this->render('index', array( 'userresult' => $userresult, 'topresult' => $topresult, 'result' => $result ));
i need pass variable $userresult
if user logged in since if user not logged in, $userresult
variable not exist. tried can not if
statement run:
return $this->render('index', array( if (!\yii::$app->user->isguest) { echo "'userresult' => $userresult"; }, 'topresult' => $topresult, 'result' => $result ));
how can achieved?
one of ways it:
// initial array $params = [ 'topresult' => $topresult, 'result' => $result, ]; // conditionally add other elements array if (!\yii::$app->user->isguest) { $params['userresult'] = $userresult } return $this->render('index', $params);
mixing echo
array wrong. should learn more arrays in plain php.
also can forget array()
syntax, use shorter variation []
since yii2 requires php >= 5.4.
and think it's better pass null
instead:
return $this->render('index', [ 'userresult' => $userresult ?: null, 'topresult' => $topresult, 'result' => $result ]);
then check if variable null
or not, or if ($userresult) { ... }
in view. think it's better using isset
in view. way params number constant.
Comments
Post a Comment