Getting Size of Float in PHP

✔ Recommended Answer

memory_get_usage() returns the amount of memory used by PHP to store the variables of your program and its internal bookeeping of these variables.

There is no direct link between the value returned by memory_get_usage() and the amount of memory used to store the actual values handled by your script.

Because the PHP variables don't have a type, each variable uses a data structure that allows it to store values of any type. This means, even for the 1-bit payload of a bool value, PHP uses a lot of memory (probably those 168 bytes you measured).

The amount is even bigger when the value is part of an array because an array is a complex type that mixes the structure and functionality of arrays, hashes and double-linked lists. In order to provide the functionality of these different data structures, the array type keeps links between the objects it stores and these links also use memory.

PHP 7 changed the way it stores its data internally and, in average, it reduced the amount of used memory to about one half of the memory used by PHP 5.

However, if you want to know how many bytes are used to store the actual float value (in order to know its range of possible values, f.e.), the information is available in the documentation page of the float type:

The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).

I suppose "platform-dependent" means it uses 4 bytes on 32-bit platforms (the traditional size for float) and 8 bytes on 64-bit and larger platforms (the traditional double).

Source: stackoverflow.com

Answered By: axiac

Method #2

In PHP, you can use the PHP_FLOAT_MAX constant to get the maximum representable finite float value on your system. This value represents the maximum size of a float in PHP.

Here's an example:

php
$floatSize = PHP_FLOAT_MAX; echo "The maximum size of a float on this system is: " . $floatSize;

This will output the maximum size of a float on your system, in scientific notation. For example:

csharp
The maximum size of a float on this system is: 1.7976931348623E+308

Note that the actual size of a float may vary depending on your system's architecture and floating-point representation.

Comments

Most Popular

PhpStorm, return value is expected to be 'A', 'object' returned

Remove Unicode Zero Width Space PHP

Laravel file upload returns forbidden 403, file permission is 700 not 755