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