size (in KB) of variable in php
is it possible to calculate / estimate the size (in KB) of a variable (string, array but mostly array). What happen is that we store some data inside memcache开发者_StackOverflow社区, and we would like to know how much memory space that data would take inside memcache.
I believe PHP's memcache implementation uses serialize when storing in memcached. You can simply serialize
the output and check it's size:
<?php
$data = array('foo' => 'bar');
$serialized_data = serialize($data);
$size = strlen($serialized_data);
# `strlen` returns number of chars in a string. Each char is 1 byte.
# So to get size in bits, multiply `strlen` results by 8. Divide by
# 1024 for KB or KiB. Divide by 1000 for kB.
print($size * 8 / 1000);
?>
精彩评论