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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Understanding Byte Size vs. Character Count in PHP

When working with string data in PHP, it is easy to conflate the number of characters stored in memory with the actual storage footprint required by the underlying encoding. If you have a variable like $Message = 'Hello, this is just test message', simply counting the characters yields 39, but determining how many bytes that text actually occupies requires understanding the current character set.

The most direct way to retrieve the size of the content in bytes is using the mb_strlen() function combined with a specific options array, or relying on strlen() if you are certain about the encoding. However, mb_strlen() defaults to counting characters (graphemes), not bytes. To get the byte count specifically, you must pass an empty options array to force it to treat the string as UTF-8 bytes, or better yet, use mb_strlen($Message, 'UTF-8', true)? No, that isn't a function signature. The correct approach for byte length in modern PHP is actually using strlen() when dealing with pure ASCII, but for international content, you need to ensure the string is treated correctly.

Actually, the most robust way to get the exact byte size of a variable in PHP, regardless of its logical character count, is to use the mb_strlen() function while explicitly specifying the encoding and ensuring you are counting bytes rather than characters. Wait, mb_strlen counts characters by default. The specific trick to get bytes for a UTF-8 string is often misunderstood.

Let's correct that. In PHP 5.4 and later, mb_strlen() accepts an optional third argument for options. However, the most reliable standard function to get byte length is simply strlen(). But if your string contains multi-byte characters (like emojis or accented letters), strlen() returns the number of bytes correctly because UTF-8 is a variable-width encoding. If $Message contains "café", strlen() will return 5 bytes (4 for 'c', 'a', 'f' plus 2 for 'é').

If you want to format this output as requested, such as displaying "Message size is 20KB", you need to calculate the raw byte count and then perform a unit conversion. Here is how you can structure that logic:

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

// Get the raw byte length
$byteSize = strlen($Message);

// Convert bytes to KB (1 KB = 1024 bytes)
$kbSize = round($byteSize / 1024, 2);

echo "<p>Message size is {$kbSize}KB</p>";

In the example above, $byteSize will be 39. Dividing by 1024 results in roughly 0.04 KB, which rounds to 0.04. This demonstrates that strlen() is safe for calculating byte size even with UTF-8 content, provided the string itself is encoded in UTF-8 and not mixed with legacy encodings like Windows-1252 without conversion.

Handling Multi-Byte Encodings

A common pitfall occurs when developers assume that the length of a string equals its byte size. This holds true for single-byte encodings (like ASCII) but fails catastrophically if you are working with UTF-8 and expect to count characters instead of bytes.

If your application handles user-generated content, you must ensure the string is valid UTF-8 before running strlen(). If the string contains invalid byte sequences, strlen() will still return a number, but that number might not represent a valid character length if you were using mb_strlen(). However, since we are strictly looking for bytes, strlen() is generally the correct tool.

If you need to count characters instead of bytes (e.g., to enforce a limit of 140 characters in a tweet-like scenario), you would use mb_strlen($Message). If you specifically need byte length for storage estimation, stick with strlen().

Formatting Output and Unit Conversion

When presenting file sizes or string sizes to users, raw numbers are rarely helpful. Developers often need to format these values into kilobytes (KB), megabytes (MB), or gigabytes (GB). The standard approach involves dividing the byte count by 1024 for KB or 1024² for MB.

Be aware of rounding errors when dealing with very small strings. As seen in the example, a string of 39 bytes results in 0.038... KB. Rounding to two decimal places yields 0.04 KB. If you are building a dashboard that displays data transfer statistics, ensure you handle the case where the division results in zero to avoid displaying "0KB" for every small payload unless that is your specific design choice.

For larger strings or files, you might encounter sizes that exceed integer limits in older PHP versions (64-bit systems use 64-bit integers, so this is rarely an issue today), but it is worth noting that strlen() returns an integer representing the byte count.

If you are working within a Laravel application and need to handle this logic across many controllers or services, consider creating a utility method. While Laravel does not have a built-in sizeInBytes helper in its core Str facade for strings specifically (it focuses on manipulation), you can easily extend the functionality if needed. However, for simple scripts, the direct calculation remains the most performant and readable solution.

Always remember that the distinction between characters and bytes is critical when calculating storage requirements or network payload sizes. Using strlen() ensures you are measuring the actual memory footprint of the string variable on a system where it is stored as UTF-8.

Our network