How to define a petabyte constant in PHP

1 Answer

0 votes
/*
    In PHP (64‑bit builds), integers are 64‑bit signed values.
    The maximum value is about 9.22 × 10^18.

    A petabyte fits comfortably:

        1 PiB = 2^50  = 1,125,899,906,842,624 bytes
        1 PB  = 10^15 = 1,000,000,000,000,000 bytes

    So both can be stored safely in a PHP integer.
*/

// Binary petabyte (pebibyte), using a bit shift.
// (1 << 50) = 2^50 bytes.
const PETABYTE_PIB = 1 << 50;

// Decimal petabyte (SI petabyte), using a readable numeric literal.
// PHP supports underscores for digit grouping.
const PETABYTE_PB = 1_000_000_000_000_000;

echo "1 PiB = " . PETABYTE_PIB . " bytes\n";
echo "1 PB  = " . PETABYTE_PB . " bytes\n";



/*
run:

1 PiB = 1125899906842624 bytes
1 PB  = 1000000000000000 bytes

*/

 



answered May 3 by avibootz
...