PHPackages                             jascree/try-cast - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jascree/try-cast

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jascree/try-cast
================

Safe type coercion for PHP – returns null on failure, no exceptions, no dependencies.

1.0.0(2mo ago)010MITPHPPHP &gt;=8.1

Since May 18Pushed 2mo agoCompare

[ Source](https://github.com/jascree/try-cast)[ Packagist](https://packagist.org/packages/jascree/try-cast)[ RSS](/packages/jascree-try-cast/feed)WikiDiscussions main Synced 3w ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

TryCast
=======

[](#trycast)

A simple, zero-dependency PHP utility for safe type coercion.

Instead of throwing exceptions on type mismatch, `TryCast` returns `null` – perfect for normalizing data from unreliable sources like HTTP requests, JSON payloads, database results, or user input.

Requirements
------------

[](#requirements)

- PHP 8.1+

Installation
------------

[](#installation)

```
composer require jascree/try-cast
```

References
==========

[](#references)

- [PHP RFC: Safe Casting Functions](https://wiki.php.net/rfc/safe_cast)

Usage
-----

[](#usage)

```
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]
```

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance86

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

67d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/47349211?v=4)[jascree](/maintainers/jascree)[@jascree](https://github.com/jascree)

---

Top Contributors

[![jascree](https://avatars.githubusercontent.com/u/47349211?v=4)](https://github.com/jascree "jascree (1 commits)")

---

Tags

normalizecoerce

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jascree-try-cast/health.svg)

```
[![Health](https://phpackages.com/badges/jascree-try-cast/health.svg)](https://phpackages.com/packages/jascree-try-cast)
```

###  Alternatives

[ergebnis/composer-normalize

Provides a composer plugin for normalizing composer.json.

1.1k41.8M3.0k](/packages/ergebnis-composer-normalize)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
