PHP code example of jascree / try-cast
1. Go to this page and download the library: Download jascree/try-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/ */
jascree / try-cast example snippets
use Jascree\TryCast\TryCast;
TryCast::toString('hello'); // 'hello'
TryCast::toString(123); // '123'
TryCast::toString(3.14); // '3.14'
TryCast::toString(null); // null
TryCast::toString(['array']); // null
TryCast::toNonEmptyString('text'); // 'text'
TryCast::toNonEmptyString(''); // null
TryCast::toNonEmptyString(0); // null
TryCast::toInt(42); // 42
TryCast::toInt('42'); // 42
TryCast::toInt('+42'); // 42
TryCast::toInt('-42'); // -42
TryCast::toInt('42.0'); // null
TryCast::toInt('42abc'); // null
TryCast::toInt(42.0); // 42
TryCast::toInt(42.5); // null
TryCast::toInt(INF); // null
TryCast::toPositiveInt(5); // 5
TryCast::toPositiveInt(0); // null
TryCast::toPositiveInt(-5); // null
TryCast::toNonNegativeInt(5); // 5
TryCast::toNonNegativeInt(0); // 0
TryCast::toNonNegativeInt(-5); // null
TryCast::toNegativeInt(-5); // -5
TryCast::toNegativeInt(0); // null
TryCast::toNonPositiveInt(-5); // -5
TryCast::toNonPositiveInt(0); // 0
TryCast::toNonPositiveInt(5); // null
TryCast::toFloat(3.14); // 3.14
TryCast::toFloat('3.14'); // 3.14
TryCast::toFloat('75e-5'); // 0.00075
TryCast::toFloat('10.0'); // 10.0
TryCast::toFloat('010'); // null
TryCast::toFloat('10abc'); // null
TryCast::toFloat(INF); // INF
TryCast::toFloat(NAN); // NAN
TryCast::toListInt(['1', 'a', 2, null, 3.0]); // [1, 2, 3]
TryCast::toListInt('not array'); // null
TryCast::toListString([1, 'hello', true, null, 3.14]); // ['1', 'hello', '3.14']
TryCast::toListFloat(['3.14', 'abc', 2, INF]); // [3.14, 2.0, INF]