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

January 06, 2016 bug library 2016-06-16

WBOY
Release: 2016-07-28 08:30:08
Original
1236 people have browsed it

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));
     }
Copy after login

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));
        }

    }
Copy after login
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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template