php - Un-Definded variable error when not logged on. -
i've error stating "notice: undefined variable: user_data" when no 1 logged in, when logged in, says hello (users name)
here piece of code echos name
"hello, <?php echo $user_data['first_name']; ?>!"
is there anyway can hello, when no user logged in, instead of error message.
thanks.
i feel previous answers not explain happening, i'll try more elaborate here.
what both of them make use of ternary operator
, refer "immediate if", can think of following snippet on 1 line:
if ($condition) { echo "this"; } else { echo "that"; }
or, ternary operator:
echo $condition ? "this" : "that"; // prints if condition true, if false.
what need solve problem, check if variable set isset
, isset checks if variable set, using in if condition, solve error.
example, , answer question:
if (isset($user_data['first_name'])) { echo 'hello, ' . $user_data['first_name']; } else { echo 'hello!'; }
so while ternary operator more convienient use here, not required solve problem, isset
in check verify set, required.
that said, both of them solve problem, it's less readable, , may little bit confusing.
Comments
Post a Comment