php 如何在PHP中获取变量内容的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11366412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the size of the content of a variable in PHP
提问by Alaa Gamal
If I have the following variable in PHP:
如果我在 PHP 中有以下变量:
$Message='Hello, this is just test message';
How can I get the size of its content in bytes? For instance, to print something like:
如何以字节为单位获取其内容的大小?例如,打印如下内容:
<p>Message size is 20KB</p>
采纳答案by SJP
$start_memory = memory_get_usage();
$foo = "Some variable";
echo memory_get_usage() - $start_memory;
This is good if you are working with any type of var.
如果您正在使用任何类型的 var,这很好。
回答by Mike Mackintosh
strlenreturns the number of bytes in the string, not the character length. View the PHP Manual here.
strlen返回字符串中的字节数,而不是字符长度。在此处查看 PHP 手册。
Look closely at:
仔细看:
Note:
strlen() returns the number of bytes rather than the number of characters in a string.
笔记:
strlen() 返回字节数而不是字符串中的字符数。
If you take the result and multiple by 8, you can get bits.
如果将结果乘以 8,则可以得到位。
Here is a function which can easily do the math for you.
这是一个可以轻松为您进行数学运算的函数。
function strbits($string){
return (strlen($string)*8);
}
Note, if you use, memory_get_usage(), you will have the wrong value returned. Memory get usage is the amount of memory allocated by the PHP script. This means, within its parser, it is allocating memory for the string and the value of the string. As a result, the value of this before and after setting a var, would be higher than expected.
请注意,如果使用, memory_get_usage(),则会返回错误的值。内存获取使用量是 PHP 脚本分配的内存量。这意味着,在其解析器中,它正在为字符串和字符串的值分配内存。因此,设置 var 前后的 this 值将高于预期。
Example, the string: Hello, this is just test message, produces the following values:
例如,字符串:Hello, this is just test message,产生以下值:
Memory (non-real): 344 bytes
Strlen: 32 Bytes
Strlen * 8bits: 256 bits
Here is the code:
这是代码:
<?php
$mem1 = memory_get_usage();
$a = 'Hello, this is just test message';
echo "Memory (non-real): ". (memory_get_usage() - $mem1)."\n";
echo "Strlen: ". strlen($a)."\n";
echo "Strlen * 8bits: ". (strlen($a) * 8)."\n";
回答by ametren
A character is one byte, so just check the string length. Divide by 1024 if you need it in KB (be prepared for a decimal).
一个字符是一个字节,所以只需检查字符串长度。如果需要,请除以 1024(以 KB 为单位)(准备小数)。
<?php echo "Message size is ".strlen($Message)."B"; ?>
回答by Madara's Ghost
strlen()returns the number of bytes in a string.
strlen()返回字符串中的字节数。
Edit: (from the PHP Manual page)
编辑:(来自 PHP 手册页)
Note:
strlen()returns the number of bytes rather than the number of characters in a string.
Note:
strlen()returns NULL when executed on arrays, and an E_WARNING level error is emitted.
注意:
strlen()返回字节数而不是字符串中的字符数。
注意:
strlen()在数组上执行时返回 NULL,并发出 E_WARNING 级别错误。
回答by Larry Hector
You should use the string length function:
您应该使用字符串长度函数:
strlen($Message)
You should also check the php manual: http://php.net/manual/en/function.strlen.php
您还应该检查 php 手册:http: //php.net/manual/en/function.strlen.php

