What's wrong with this notation? Session cannot be used in ternary expressions?
$data['user_id'] = isset(session('user_id')):session('user_id'):'guest';
Reply content:
What's wrong with this notation? Session cannot be used in ternary expressions?
$data['user_id'] = isset(session('user_id')):session('user_id'):'guest';
Suggest you write like this
$data['user_id'] = session('user_id');if (empty($data['user_id'])) $data['user_id'] = 'guest';
Three-way proposal for
$data['user_id'] = session('user_id') ? session('user_id') : 'guest';
Isset is used to detect if a variable has been declared, rather than to check the value, otherwise a syntax error is generated.
Three-dimensional expression
condition ? p1 : p2
You're all two of them :
.
Http://php.net/manual/zh/language.operators.comparison.php#language.operators.comparison.ternary
You wrote two: $data [' user_id '] = Session (' user_id ')? Session (' user_id '): ' guest ';
$data['user_id'] = isset(session('user_id'))?session('user_id'):'guest';
First of all, as stated above, judging the conditions should be? No.
Second, the Isset parameter cannot be a function return value