【问题标题】:How can i find the size of a intger/char in bytes using php? [duplicate]如何使用 php 找到整数/字符的大小(以字节为单位)? [复制]
【发布时间】:2012-06-27 17:37:20
【问题描述】:

可能重复:
Is there a way I get the size of a PHP variable in bytes?

我需要使用 php 查找 int/char 或数组的字节大小。我认为它可以在 php 中进行内存管理。

【问题讨论】:

  • IIRC,php不使用整数类型,而是使用浮点数。
  • @Wug:不,PHP 有独立的 Int 和 Float 类型。其他类型(例如 Real)是 Float 的化名。
  • 那么必须是不同的语言。我在想什么...编辑:我认为它的 javascript。

标签: php


【解决方案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;

【讨论】:

  • 你试过了吗?因为它行不通!
  • 如果我把 $temp = str_repeat() 它计数正确,但如果我只是复制它得到一个负值?在 php 中优化?还是为什么?
  • $temp = $variable_to_test 被称为浅拷贝,在你修改$temp 之前,它实际上不会在内存中占有一席之地。您可以采用以下两种方法之一:1- $temp = $variable_to_test;$temp .= ''; 2- $temp = unserialize(serialize($variable_to_test))
  • 好的,感谢您的解释,在上面的代码中添加一个空字符串的连接,现在它的值更高(可能是正确的)
猜你喜欢
  • 1970-01-01
  • 2013-02-06
  • 2012-02-01
  • 2011-11-26
  • 2019-07-20
  • 2013-04-26
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多