PHP code example of sj-i / php-cast

1. Go to this page and download the library: Download sj-i/php-cast 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/ */

    

sj-i / php-cast example snippets


use PhpCast\Cast;
use PhpCast\NullableCast;

// int(1)
$int_value = Cast::toInt('1');
// string(1) "1"
$string_value = Cast::toString(1);
// float(1)
$float_value = Cast::toFloat(1);
// bool(true)
$bool_value = Cast::toBool(1);

// TypeError
$int_value = Cast::toInt('a');
// TypeError
$int_value = Cast::toInt(null);


// int(1)
$int_value = NullableCast::toInt('1');
// string(1) "1"
$string_value = NullableCast::toString(1);
// float(1)
$float_value = NullableCast::toFloat(1);
// bool(true)
$bool_value = NullableCast::toBool(1);

// TypeError
$null_value = NullableCast::toInt('');
// null
$null_value = NullableCast::toInt(null);