Re: Bug in array_key_exist?

From: Date: Wed, 27 Aug 2008 00:30:02 +0000
Subject: Re: Bug in array_key_exist?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-278750@lists.php.net to get a copy of this message
Korgan wrote:
Hi, I have a problem with array_key_exists in if statement. I have a class with this function class XXX {
    private items = array();
... ... ... public function addXXX($id, $count)
    {
       $count = (int)$cout;
     Let me point at it ^^^^
Check your spelling If error_reporting was set to E_ALL AND display_errors were turned on, you would see that you are using an undefined variable in your method called $cout, casting is as an int and assigning the resulting value (which would always be zero) to $count.
        if (!array_key_exists($id, $this->items))
           $this->items[$id] = $count;
        else
           $this->items[$id] += $count;
    }
... ... } And I want to send instance of this class with SESSION. If I add a item to the array, count is ok, but if i go to the next page count will change. There is the code of index.php /** its loading classes ***/ spl_autoload_register('loadClass'); session_start(); var_dump($_SESSION['XX']); /** A **/ .... ... ... ... if ($_SESSION['XX'] instanceof XXX)
    $x = $_SESSION['XX'];
else
    new...
.. .. /** Do onzl this **/ if ....
    $x->addXXX($id, $cnt);
.... ... ... $_SESSION['XX'] = X; var_dump($_SESSION['XX']); /** B **/ There is a vardump if action addXXX exec: A:
    object(Kosik)#1 (1) { ["zbozi:private"]=>  array(0) { } }
B:
    object(Kosik)#1 (1) { ["zbozi:private"]=>  array(1) { [1]=>  int(1) } }
And than i will go to some page and vardums are: A:
    object(Kosik)#1 (1) { ["zbozi:private"]=>  array(1) { [1]=>  int(4) } } 1 : 4
B:
     object(Kosik)#1 (1) { ["zbozi:private"]=>  array(1) { [1]=>  int(4) } }
I am thinking that do both codes in if statement, but $this->items[$id] += $count; exec after send page. I am using PHP Version 5.2.6 and Smarty v.2.6.19 Thanks for help :)


« previous php.general (#278750) next »