Alaa Gamal
Alaa Gamal

Reputation: 1125

How to get the size of the content of a variable in PHP

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>

Upvotes: 30

Views: 67186

Answers (6)

Anton Sukhachev
Anton Sukhachev

Reputation: 139

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

Upvotes: 2

Mike Mackintosh
Mike Mackintosh

Reputation: 14237

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";

Upvotes: 62

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

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.

Upvotes: 5

ametren
ametren

Reputation: 2246

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"; ?>

Upvotes: 8

Larry Hector
Larry Hector

Reputation: 183

You should use the string length function:

strlen($Message)

You should also check the php manual: http://php.net/manual/en/function.strlen.php

Upvotes: 1

SJP
SJP

Reputation: 1372

$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.

Upvotes: 12

Related Questions