Home >Backend Development >PHP Tutorial >January 06, 2016 bug library 2016-06-16
1. About array_search and unset
Original code:
public function appViewUnread($uid, $id) { $userNewRepyKey = Config_CacheKey::USER_QUESTION_NEW_REPLY.$uid; $userNewReply = $this->appGetUserNewReply($uid); $key = array_search($id, $userNewReply); unset($userNewReply[$key]); $this->redis->set($userNewRepyKey, serialize($userNewReply)); }
BUG analysis: If array_search cannot find the result, it returns false, then unset ($userNewReply[false]) is equivalent to unset($ userNewRrply[0]) , the first element of the array will be deleted.
Updated code:
public function appViewUnread($uid, $id) { $userNewRepyKey = Config_CacheKey::USER_QUESTION_NEW_REPLY.$uid; $userNewReply = $this->appGetUserNewReply($uid); $key = array_search($id, $userNewReply); if($key != false) { unset($userNewReply[$key]); $this->redis->set($userNewRepyKey, serialize($userNewReply)); } }summary: For situations where the function return value may be false, the return result must be verified.
The above introduces the bug library 2016-06-16 on January 6, 2016, including the content on January 6, 2016. I hope it will be helpful to friends who are interested in PHP tutorials.