PHP code example of oceanmoon / quantities

1. Go to this page and download the library: Download oceanmoon/quantities 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/ */

    

oceanmoon / quantities example snippets


use OceanMoon\Quantities\PhysicalConstant;
use OceanMoon\Quantities\QuantityType\Angle;
use OceanMoon\Quantities\QuantityType\Force;
use OceanMoon\Quantities\QuantityType\Frequency;
use OceanMoon\Quantities\QuantityType\Length;
use OceanMoon\Quantities\QuantityType\Mass;
use OceanMoon\Quantities\QuantityType\Money;
use OceanMoon\Quantities\QuantityType\Temperature;

// Create measurements
$distance = new Length(5, 'km');
$temp = new Temperature(25, 'degC');
$angle = new Angle(90, 'deg');
$price = new Money(4269, 'EUR');

// Convert between units
$miles = $distance->to('mi');        // 3.10686... miles
$fahrenheit = $temp->to('degF');     // 77°F
$radians = $angle->to('rad');        // 1.5707... rad
$dollars = $price->to('USD');        // e.g. 4621 USD

// Convert to SI units
$force = new Force(28000, 'lbf');
$force = $force->toSi();      // 124... kN
$force = $force->toSiBase();  // 124550... kg⋅m/s²

// Arithmetic operations
$total = $distance->add(new Length(500, 'm'));      // 5.5 km
$doubled = $distance->mul(2.0);                     // 10 km
$period = new Frequency(2.4, 'GHz')->inv()->toSi(); // 416.7... ps

// Physical constants
$h = PhysicalConstant::planck();
$c = PhysicalConstant::speedOfLight();

// Scientific and engineering calculations
$G = PhysicalConstant::gravitational();
$earthMass = new Mass(5.972e24, 'kg');
$moonMass = new Mass(7.342e22, 'kg');
$earthMoonDistance = new Length(3.844e8, 'm');
$gravity = $G->mul($earthMass)->mul($moonMass)->div($earthMoonDistance->sqr());
echo $gravity->to('N')->format('e', 2), "\n"; // 1.98×10²⁰ N

// Parse from strings
$length = Length::parse('123.45 km');
$temp = Temperature::parse('98.6°F');
$angle = Angle::parse("45° 30' 15\"");

// Format as parts
$angle = new Angle(45.5042, 'deg');
echo $angle->formatParts(precision: 1);
// "45° 30′ 15.1″"