wppaste
WordPress

size_format( int|string $bytes, int $decimals = 0 ): string|false

Since
2.3.0, 6.0.0
Source
wp-includes/functions.php:467
Converts a number of bytes to the largest unit the bytes will fit into.

Description

It is easier to read 1 KB than 1024 bytes and 1 MB than 1048576 bytes. Converts number of bytes to human readable number by taking the number of that unit that the bytes will go into it. Supports YB value.

Please note that integers in PHP are limited to 32 bits, unless they are on 64 bit architecture, then they have 64 bit size. If you need to place the larger size then what PHP integer type will hold, then use a string. It will be converted to a double, which should always have 64 bit length.

Technically the correct unit names for powers of 1024 are KiB, MiB etc.

Compatibility

WordPress
since 6.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$bytesint|string
Number of bytes. Note max integer size for integers.
$decimalsintoptional
Precision of number of decimal places. Default 0.Default: 0

Return value

string|false
Number string on success, false on failure.

Performance profile

How much work a call to size_format() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
61–73

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 86.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
15

15 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below size_format()

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost size_format() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$bytes !== 061–62_x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x()
$bytes === 069_x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), number_format_i18n(), _x()
$bytes !== 0 && !$quant && $mag73_x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), number_format_i18n()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 86 instructions, 61–73 executed per call, 4 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

  • _x()Retrieves translated string with gettext context.
  • number_format_i18n()Converts float number to format based on the locale.

Used by · 15

Show all 15

Source code

function size_format( $bytes, $decimals = 0 ) {	$quant = array(		/* translators: Unit symbol for yottabyte. */		_x( 'YB', 'unit symbol' ) => YB_IN_BYTES,		/* translators: Unit symbol for zettabyte. */		_x( 'ZB', 'unit symbol' ) => ZB_IN_BYTES,		/* translators: Unit symbol for exabyte. */		_x( 'EB', 'unit symbol' ) => EB_IN_BYTES,		/* translators: Unit symbol for petabyte. */		_x( 'PB', 'unit symbol' ) => PB_IN_BYTES,		/* translators: Unit symbol for terabyte. */		_x( 'TB', 'unit symbol' ) => TB_IN_BYTES,		/* translators: Unit symbol for gigabyte. */		_x( 'GB', 'unit symbol' ) => GB_IN_BYTES,		/* translators: Unit symbol for megabyte. */		_x( 'MB', 'unit symbol' ) => MB_IN_BYTES,		/* translators: Unit symbol for kilobyte. */		_x( 'KB', 'unit symbol' ) => KB_IN_BYTES,		/* translators: Unit symbol for byte. */		_x( 'B', 'unit symbol' )  => 1,	); 	if ( 0 === $bytes ) {		/* translators: Unit symbol for byte. */		return number_format_i18n( 0, $decimals ) . ' ' . _x( 'B', 'unit symbol' );	} 	foreach ( $quant as $unit => $mag ) {		if ( (float) $bytes >= $mag ) {			return number_format_i18n( $bytes / $mag, $decimals ) . ' ' . $unit;		}	} 	return false;}

Changelog

Introduced in 2.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.0.0
Support for PB, EB, ZB, and YB was added.from the docblock
2.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.