PHPackages                             squirrelphp/scalar-types - 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. squirrelphp/scalar-types

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

squirrelphp/scalar-types
========================

Make the coercive mode and strict mode behavior of PHP accessible explicitly via functions.

v0.2.1(3y ago)06MITPHPPHP &gt;=7.2

Since May 31Pushed 3y ago1 watchersCompare

[ Source](https://github.com/squirrelphp/scalar-types)[ Packagist](https://packagist.org/packages/squirrelphp/scalar-types)[ Docs](https://github.com/squirrelphp/scalar-types)[ RSS](/packages/squirrelphp-scalar-types/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

Squirrel Scalar Types
=====================

[](#squirrel-scalar-types)

[![Packagist Version](https://camo.githubusercontent.com/16e2bd3b794725d6f18823467e5f648eccf47c2cad209b2107a18f60881e408c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f737175697272656c7068702f7363616c61722d74797065732e7376673f7374796c653d666c61742d726f756e64)](https://packagist.org/packages/squirrelphp/scalar-types) [![PHP Version](https://camo.githubusercontent.com/916bcaf89c458cbe54caa9361ca1a233f49d6ad1525fa7b7f5b78362866de6ed/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f737175697272656c7068702f7363616c61722d74797065732e737667)](https://packagist.org/packages/squirrelphp/scalar-types) [![Software License](https://camo.githubusercontent.com/4f9c8f8c009336d7177cf697d694e2e763e5e707f1a6224ab0d295465eadffbd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d737563636573732e7376673f7374796c653d666c61742d726f756e64)](LICENSE)

Make the coercive mode and strict mode behavior of PHP accessible explicitly via functions: Includes coercions and is\_coerceable checks that work like the implicit scalar type coercions in PHP, and an easy way to enforce a specific scalar type mimicking the behavior of strict mode in PHP.

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

[](#installation)

```
composer require squirrelphp/scalar-types

```

Table of contents
-----------------

[](#table-of-contents)

- [Test if value can be coerced](#test-if-value-can-be-coerced)
- [Coerce value](#coerce-value)
- [Enforce a type for a value](#enforce-a-type-for-a-value)

Test if value can be coerced
----------------------------

[](#test-if-value-can-be-coerced)

All these functions have one mixed argument `$value` and return true or false:

### is\_coerceable\_to\_int

[](#is_coerceable_to_int)

Returns true if `$value` is one of the following:

- An integer
- A float without fractional part
- A numeric string without fractional part
- A boolean

For any other values it returns false.

### is\_coerceable\_to\_float

[](#is_coerceable_to_float)

Returns true if `$value` is one of the following:

- An integer
- A float
- A numeric string
- A boolean

For any other values it returns false.

### is\_coerceable\_to\_bool

[](#is_coerceable_to_bool)

Returns true if `$value` is one of the following:

- An integer with value 0 or 1
- A float with value 0 or 1
- An empty string
- A string with value '0' or '1'
- A boolean

For any other values it returns false.

### is\_coerceable\_to\_string

[](#is_coerceable_to_string)

Returns true if `$value` is one of the following:

- An integer
- A float
- A string
- A boolean
- An object with the magic method \_\_toString

For any other values it returns false.

Coerce value
------------

[](#coerce-value)

All these functions have one mixed argument `$value` and return the type they are coercing to, following the same logic as implicit type coercions in PHP.

### coerce\_to\_int

[](#coerce_to_int)

Coerce a value like PHP 8.2 would do it, which can lead to a TypeError for non-scalar and non-numeric values and a deprecation notice for a float or numeric string with a fractional part.

### coerce\_to\_float

[](#coerce_to_float)

Coerce a value like PHP 8.2 would do it, which can lead to a TypeError for non-scalar and non-numeric values.

### coerce\_to\_bool

[](#coerce_to_bool)

Coerce a value like PHP 8.2 would do it, which can lead to a TypeError for non-scalar values and a deprecation notice for values other than "", "0", "1", 0, 1, 0.0 and 1.0.

### coerce\_to\_string

[](#coerce_to_string)

Coerce a value like PHP 8.2 would do it, which can lead to a TypeError for non-scalar values except if it is an object that implements the magic \_\_toString method.

Enforce a type for a value
--------------------------

[](#enforce-a-type-for-a-value)

All these functions have one mixed argument `$value` and return the type they are enforcing, according to the same logic as strict mode in PHP.

### enforce\_int

[](#enforce_int)

Returns `$value` as an integer if it is an integer. Throws a TypeError otherwise.

### enforce\_float

[](#enforce_float)

Returns `$value` as a float if it is an integer or a float. Throws a TypeError otherwise.

### enforce\_bool

[](#enforce_bool)

Returns `$value` as a boolean if it is a boolean. Throws a TypeError otherwise.

### enforce\_string

[](#enforce_string)

Returns `$value` as a string if it is a string. Throws a TypeError otherwise.

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 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

Every ~0 days

Total

3

Last Release

1447d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8cd200dc74af2d77e8c480000a7e3bab0b8a98fae4b399d306b3d332354f8177?d=identicon)[iquito\_ch](/maintainers/iquito_ch)

---

Top Contributors

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

---

Tags

phpscalarcoercions

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/squirrelphp-scalar-types/health.svg)

```
[![Health](https://phpackages.com/badges/squirrelphp-scalar-types/health.svg)](https://phpackages.com/packages/squirrelphp-scalar-types)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

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