PHP code example of unit / information

1. Go to this page and download the library: Download unit/information library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

unit / information example snippets


use Unit\Information\Size;

// From bits
$bits = 8;
$size = new Size($bits);

// ... or from other units (internally other units are converted to bits)
$size = Size::fromBytes(1);
$size = Size::fromMegabytes(10);
// Avoid an exception on invalid values (that means values that cannot be converted to bits):
$size = Size::fromBytes(1.111, false);

// ... or from strings (internally strings are transformed to bits)
$size = Size::fromString('1b');
$size = Size::fromString('1B');
$size = Size::fromString('1MB');
$size = Size::fromString('1KiB');
$size = Size::fromString('0.5GB');

// ... or from PHP shorthand value (https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes)
$size = Size::fromPhpShorthandValue('1M'); // 1048576 bytes

// ... or from ini settings
try {
    $size = Size::fromMemoryLimit(); // Calls ini_get('memory_limit')
} catch (\Unit\Information\Exception\NoMemoryLimitException $exception) {
    // For example if memory_limit is "-1"
}
$size = Size::fromUploadMaxFilesize(); // Calls ini_get('upload_max_filesize')
$size = Size::fromRealMemoryUsage(); // Calls memory_get_usage(true)

// ... or from directories or files
$size = Size::fromDirectoryOrFile('/var/foobar');

use Unit\Information\Size;
use Unit\Information\Unit;

// Default format uses nearest unit and returns a human-readable string
Size::fromBytes(1)->format();              // "1B"
Size::fromBytes(141642)->format();         // "141.6kB"
Size::fromBytes(14164234235235)->format(); // "14.2TB"

// Specific unit
Size::fromBytes(1)->format(Unit::Bit);                       // "8b"
Size::fromBytes(1)->format(Unit::Byte);                      // "1B"
Size::fromBytes(1)->format(Unit::Kilobyte);                  // "0.0kB"
Size::fromBytes(1)->format(Unit::Kilobyte, precision: null); // "0.001kB"
Size::fromBytes(73042346800)->format(Unit::Megabyte);        // "73042MB"
Size::fromBytes(300000)->format(Unit::Megabyte, 1);          // "0.3MB"

// Specific precision
Size::fromBytes(73042346800)->format(precision: null); // "73.0423468GB"
Size::fromBytes(73042346800)->format(precision: 0);    // "73GB"
Size::fromBytes(73042346800)->format(precision: 1);    // "73.0GB"
Size::fromBytes(73042346800)->format(precision: 2);    // "73.04GB"

// Custom format
Size::fromBytes(1)->format(format: '%value% %unit_full_name% (%unit%)'); // "1 Byte (B)"

use Unit\Information\Size;
use Unit\Information\Unit;

Size::fromBytes(100000)->get(Unit::Kilobyte); // 100
Size::fromBytes(1)->get(Unit::Kilobyte);      // 0.001

use Unit\Information\Size;

$size = Size::fromString('1MB');
$otherSize = Size::fromString('1MB');

// Objects are immutable (all operations return a new instance)
$newSize = $size->add($otherSize); // $size is still 1M, $otherSize is still 1M
$newSize = $size->subtract($otherSize);
$newSize = $size->multiply($otherSize);
$newSize = $size->divide($otherSize);

// Chainable
$newSize = $size->add($otherSize)->subtract($otherSize);