PHP function memcache_decrement gives strange output
mikael at synd.info
Sat Aug 18 08:13:54 UTC 2007
This seems to be a memcached issue, a quick telnet session shows that
setting a value to for example "10" and then fetching the same value
returns a two byte space padded response like "9 ", most likely due to
the fact that memcached stores and modifies counters just like any other
string value and does not adjust the item length on decrement.
To get around this problem in PHP you could cast the result to (int) if
you know you're fetching a counter, or use the return value from
memcache_decrement() which is the new number but integer converted.
Sumit Datta wrote:
> I have come across an issue with memcache_decrement in PHP. I guess
> this same will happen with clients in other languages so I am posting
> it here. The problem is pretty simple:
> * set a key to value 0
> * use increment to raise the value (increment by 1 as in default)
> * I do a var_dump in PHP which shows normal stuff, which is a string
> representing the number. (Of course it should have been an integet
> output, but at least the value if OK).
> * use decrement to lower the value (decrement by 1 as in default)
> * I do a var_dump again and here the outputs are not usual. When value
> reduces in the number of digits there appears an extra <space> at the
> end of the string shown by var_dump.
> Here is a demo code:
> the you can check the result:
> In this code even with the <space> memcache gets data pretty easily.
> This actually caused data fetch errors in one of our codes. Which led
> me to this issue. I am not sure why this happens. If i use intval( )
> of the needed decremented data from memcache all works fine.
> Is this a PHP only error?
> =Code is Poetry=
> Sumit Datta
> Web Developer
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 250 bytes
Desc: OpenPGP digital signature
Url : http://lists.danga.com/pipermail/memcached/attachments/20070818/324249ef/signature.pgp
More information about the memcached