size_format( int|string $bytes, int $decimals = 0 ): string|false
- Since
- 2.3.0, 6.0.0
- Source
wp-includes/functions.php:468
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
- Scaling
- Scales with input
- Instructions
- 61–73
- Plugin surface
- None
- Called by
- 14
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 86.
Nothing here hands control to plugin code.
14 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
$bytes !== 0 | 61–62 | _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x() |
$bytes === 0 | 69 | _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), _x(), number_format_i18n(), _x() |
$bytes !== 0 && !$quant && $mag | 73 | _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 · 14
- WP_Debug_Data::get_sizes()Fetches the sizes of the WordPress directories: `wordpress` (ABSPATH), `plugins`, `themes`, and `uploads`.
- WP_Debug_Data::get_wp_media()Gets the WordPress media section of the debug data.
- WP_Site_Health::get_test_autoloaded_options()Tests the number of autoloaded options.
- WP_Site_Health::get_test_available_updates_disk_space()Tests available disk space for updates.
- attachment_submitbox_metadata()Displays non-editable attachment metadata in the publish meta box.
- display_space_usage()Displays the amount of disk space used by the current site. Not used in core.
- media_upload_form()Outputs the legacy media upload form.
- multisite_over_quota_message()Displays the out of storage quota message in Multisite.
- upload_is_user_over_quota()Checks whether a site has used its allotted upload space.
- wp_import_upload_form()Outputs the form used by the importers to accept the data to be imported.
- wp_prepare_attachment_for_js()Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.
- wp_print_media_templates()Prints the templates used in the media manager.
Show all 14
- wp_xmlrpc_server::mw_newMediaObject()Uploads a file, following your settings.
- wpmu_checkAvailableSpace()Determines if the available space defined by the admin has been exceeded by the user.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.