PHP code example of granam / integer

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

    

granam / integer example snippets



use Granam\Integer\IntegerObject;
use Granam\Integer\Tools\Exceptions\WrongParameterType;

$integer = new IntegerObject(12345);

// int(12345)
var_dump($integer->getValue());

$integerFromString = new IntegerObject('124578');
// int(124578)
var_dump($integerFromString->getValue());

$integerFromFloatString = new IntegerObject('987.0');
// int(987)
var_dump($integerFromFloatString->getValue());

try {
    new IntegerObject(987.123);
} catch (WrongParameterType $integerException) {
   // Something get wrong: Some value has been lost on cast. Got '987.456', cast into integer 987
   echo('Something get wrong: ' . $integerException->getMessage() . "\n");
 }

$integerFromTrue = new IntegerObject(true);
// int(1)
var_dump($integerFromTrue->getValue());

$integerFromNull = new IntegerObject(null);
// int(0)
var_dump($integerFromNull->getValue());
// ...

$stringWithAlmostInteger = '0.9999999999999999';
$integerFromStringWithAlmostInteger = new IntegerObject($stringWithAlmostInteger);
// int(1)
var_dump($integerFromStringWithAlmostInteger->getValue());
// int(0) -- because of (int)(float)$value
var_dump((int)$stringWithAlmostInteger);