thinkphp $this->reset && session ($key, NULL); What does this mean?

Source: Internet
Author: User
public function check($code, $id = '') {        $key = $this->authcode($this->seKey).$id;        // 验证码不能为空        $secode = session($key);        if(empty($code) || empty($secode)) {            return false;        }        // session 过期        if(NOW_TIME - $secode['verify_time'] > $this->expire) {            session($key, null);            return false;        }        if($this->authcode(strtoupper($code)) == $secode['verify_code']) {            $this->reset && session($key, null);//这个地方为什么用&&,而不是三元运算符?            return true;        }        return false;    }

My understanding is that it should be written like this:

$this->reset ? session($key, null):1;

Reply content:

public function check($code, $id = '') {        $key = $this->authcode($this->seKey).$id;        // 验证码不能为空        $secode = session($key);        if(empty($code) || empty($secode)) {            return false;        }        // session 过期        if(NOW_TIME - $secode['verify_time'] > $this->expire) {            session($key, null);            return false;        }        if($this->authcode(strtoupper($code)) == $secode['verify_code']) {            $this->reset && session($key, null);//这个地方为什么用&&,而不是三元运算符?            return true;        }        return false;    }

My understanding is that it should be written like this:

$this->reset ? session($key, null):1;

Depending on the execution rules of &&, if the left side returns true, the right action is performed, otherwise the right action is not performed. This will not be the second floor to say that is not standard, it will not be the first floor said with if code too much.

is actually a lame way of writing, although the execution result and the ternary operator, no advantage
The readability is too poor.

Do not agree with the downstairs of the writing, since there is a ternary operator, why use if?
One line of code can fix the problem, why use 3 lines?

You can see it as ternary arithmetic, but even if you think of it as a ternary, it is an irregular notation.

Its reasonable wording should be

if ($this->reset) {    session($key, null);}

Agree with @zj to fly higher students ' answers.

In practice, however, the recommended and and the or operators have lower priority and are more suitable for linking to statements.

More important is the readability is not low ah, and is "do ...", or is "otherwise do ..."

Commonly known as short-circuit operations
An If statement that replaces the simplified content body with only one sentence.

Do not think this is an irregular way of writing, the existence is reasonable, the use of this operator is this, but most of the use of conditional judgment

Remember where to see this writing efficiency is higher than if, not measured, although this performance efficiency can be ignored
Sometimes if nested many, but inside the content but only one sentence, with this or three mesh operation readability will be better

  • Related Article

    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.