2

Possible Duplicate:
Is there a way I get the size of a PHP variable in bytes?

I need to find the size in bytes using php for int/char or an array . I think its possible with memory management in php .

Community
  • 1
  • 1
  • IIRC, php does not use integer types, but uses floating point for everything. – Wug Jun 27 '12 at 17:39
  • 1
    @Wug: No, PHP has separate Int and Float types. Other types (Real, for instance) are pseudonyms for Float. – dotancohen Jun 27 '12 at 17:42
  • must be a different language then. what am I thinking of... edit: I think its javascript. – Wug Jun 27 '12 at 17:44
  • take a look at http://stackoverflow.com/questions/2192657/how-to-determine-the-memory-footprint-size-of-a-variable – loler Jun 28 '12 at 06:01

1 Answers1

1
/* load before whit a value */
$before = memory_get_usage(FALSE);

/* make sure $temp dosn't have a content before */
$temp = NULL;

/* calculate current usage */
$before = memory_get_usage(FALSE);

/* copy the content to a nex varibale */
$temp = $variable_to_test . '';

/* calculate the new usage */
$after = memory_get_usage(FALSE);

/* clean up data */
unset($temp);

/* calculate the incresed memory usage */
$memory_usage = $after - $before;
Puggan Se
  • 5,738
  • 2
  • 22
  • 48
  • 3
    did you try it? Because it won't work! – Adi Jun 27 '12 at 17:59
  • if i put a $temp = str_repeat() it counts correct, but if i just copies that its get a negative value? optimizen in php? or why? – Puggan Se Jun 27 '12 at 20:23
  • 1
    `$temp = $variable_to_test` is called shallow copying, it won't actually take a place in the memory before you modify `$temp`. You can take one of two approaches: 1- `$temp = $variable_to_test;$temp .= '';` 2- `$temp = unserialize(serialize($variable_to_test))` – Adi Jun 27 '12 at 21:37
  • ok, thanks for explaining that, addad a concatination whit an empty string to the code above, and now it gets higher (probely correct) values – Puggan Se Jun 28 '12 at 05:36