Home >Backend Development >PHP Tutorial >January 06, 2016 bug library 2016-06-16

January 06, 2016 bug library 2016-06-16

WBOY
WBOYOriginal
2016-07-28 08:30:081237browse

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn