30

If I have the following variable in 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>
0

6 Answers 6

62

strlen returns the number of bytes in the string, not the character length. View the PHP Manual here.

Look closely at:

Note:

strlen() returns the number of bytes rather than the number of characters in a string.

If you take the result and multiple by 8, you can get bits.

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.

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
  • What is faster if I do not need an accurate result, strlen or memory_get_usage?
    – mgutt
    Apr 15, 2015 at 0:21
  • But how to get size of an arrays or objects?
    – Čamo
    Oct 14, 2019 at 7:41
  • You can use the memory_get_usage() solution if you are dealing with complex objects and not strings. One might suggest that you can serialize() your object, but that's a crappy approach imo since serialization uses arbitrary characters for field delineation and type handling and takes away the memory footprint required by certain types (strings != int != float != etc). Oct 14, 2019 at 8:22
10
$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.

7
  • 17
    This is a bit off. memory_get_usage() returns the amount of memory, in bytes, that's currently being allocated to your PHP script. Your script allocated memory to set the variable $foo as well as memory to set the variable $foo to a string and append a value. The result is higher than the actual bit/byte count of the string. Jul 6, 2012 at 17:32
  • 4
    then why does it always return 272 both when I use "Some variable" as string or "Some variable Some variable Some variable" (many times)? Even if I assign empty string it still shows 272. It seems that this method is not accurate
    – vladkras
    Sep 5, 2014 at 16:05
  • 1
    Incorrect, does not take into account storage overheads as it calls generic PHP script memory usage. Sep 20, 2016 at 10:22
  • This is highly inaccurate if used in an actual program or even if the variable is already in use. Sep 27, 2017 at 14:33
  • 2
    Why is this checked, this does not answer the question.
    – akronymn
    Mar 8, 2018 at 19:22
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).

<?php echo "Message size is ".strlen($Message)."B"; ?>
2
  • 3
    only one who said divide by 1024 for getting KB, i believe everyone know this but no one mention it , up voted
    – Gntem
    Jul 6, 2012 at 16:56
  • 1
    yup, the string given is about .03KB
    – ametren
    Jul 6, 2012 at 16:58
5

strlen() returns the number of bytes in a string.

Edit: (from the PHP Manual page)

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.

4
  • Would the length be equal to the size in bytes? a character is 8 bits or not?
    – Sena
    Jul 6, 2012 at 16:54
  • A character is 1 byte (8 bits).
    – ametren
    Jul 6, 2012 at 16:54
  • 2
    @ametren: Not always, multibyte strings (Unicode encoded) and may contain characters with more than 1 byte. Jul 6, 2012 at 16:58
  • 2
    @ametren: Counts the bytes, not the nubmer of characters, which is exactly what OP wants. Jul 6, 2012 at 17:03
2

You can use https://github.com/mrsuh/php-var-sizeof
It provides function var_sizeof() for getting size of any PHP variable in bytes.
It must be more accurate tool to calculate total size of PHP variable than memory_get_usage().

<?php

require_once __DIR__ . '/vendor/autoload.php';

$message='Hello, this is just test message';

printf("\$message size is %d bytes\n", var_sizeof($message));
$message size is 48 bytes
1
  • Currently this throws Warning: Uncaught RuntimeException: Unsupported machine type in vendor/mrsuh/php-var-sizeof/src/VarInfo.php:16 on an M1. ("mrsuh/php-var-sizeof": "^1.2") Oct 12, 2023 at 5:05
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.