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>
5 个解决方案
#1
5
$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,这都很好。
#2
39
strlen
returns 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之前和之后的值将高于预期值。
Example, the string: Hello, this is just test message
, produces the following values:
例如,字符串:您好,这只是测试消息,产生以下值:
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";
#3
8
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).
字符是一个字节,所以只需检查字符串长度。如果你需要以KB为单位除以1024(准备小数)。
<?php echo "Message size is ".strlen($Message)."B"; ?>
#5
1
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
#1
5
$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,这都很好。
#2
39
strlen
returns 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之前和之后的值将高于预期值。
Example, the string: Hello, this is just test message
, produces the following values:
例如,字符串:您好,这只是测试消息,产生以下值:
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";
#3
8
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).
字符是一个字节,所以只需检查字符串长度。如果你需要以KB为单位除以1024(准备小数)。
<?php echo "Message size is ".strlen($Message)."B"; ?>
#4
#5
1
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