Thinkphp 3.2.3 What is wrong with the notation of this ternary expression?

Source: Internet
Author: User
Keywords thinkphp php
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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.