PHPackages                             1tomany/nullify - 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. 1tomany/nullify

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

1tomany/nullify
===============

A very simple library to trim and nullify strings

v0.0.3(yesterday)04↑2900%MITPHPPHP &gt;=8.4

Since Aug 8Pushed yesterdayCompare

[ Source](https://github.com/1tomany/nullify)[ Packagist](https://packagist.org/packages/1tomany/nullify)[ RSS](/packages/1tomany-nullify/feed)WikiDiscussions master Synced today

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

String Nullifier for PHP
========================

[](#string-nullifier-for-php)

This simple library exposes a single function named `nullify()` in the global namespace. By default, the function trims the string passed into it and returns `null` if the trimmed value is an empty string, or the trimmed string otherwise.

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

[](#installation)

```
composer require 1tomany/nullify

```

Usage
-----

[](#usage)

The `nullify()` function has the following signature:

```
nullify(string|\Stringable|null $string, bool $trim = true): ?string;
```

If the `$trim` argument is set to `false`, the string is not trimmed. An empty string will still return `null`, otherwise, the original string is returned. If an object implementing `\Stringable` is passed in, the object will be converted to a `string` first.

Purpose
-------

[](#purpose)

Dealing with falsy and empty values in PHP has always been a challenge. If you've used the language long enough, you've probably been bitten by `empty('0')` returning `true` even though the string `'0'` clearly isn't empty. My preference is to typehint strings as `non-empty-string|null` to clearly indicate what kinds of values that variable holds.

Take the following basic DTO as an example:

```
final readonly class Person
{
    public function __construct(
        private ?string $name,
    ) {
    }

    public function getName(): ?string
    {
        return $this->name;
    }
}
```

Because of the ambiguity of the `$name` property, it's cumbersome to determine if it is empty or not:

```
$person = new Person('     ');

if (null === $person->getName() || '' === trim($person->getName())) {
    throw new \InvalidArgumentException('Please enter a name.');
}
```

For all practical purposes, `$name` is empty, but determining that is cumbersome.

A much clearer DTO would be written like this:

```
final readonly class Person
{
    /**
     * @var ?non-empty-string
     */
    private ?string $name;

    public function __construct(
        ?string $name,
    ) {
        $this->name = nullify($name);
    }

    /**
     * @return ?non-empty-string
     */
    public function getName(): ?string
    {
        return $this->name;
    }
}
```

It's immediately clear what data `$name` holds, and testing if `$name` is empty or not is a cinch:

```
$person = new Person('     ');

if (null === $person->getName()) {
    throw new \InvalidArgumentException('Please enter a name.');
}
```

These are also clear because `nullify()` has removed any ambiguity around falsy or empty values:

```
if (!$person->getName()) {
}

if (empty($person->getName())) {
}
```

Examples
--------

[](#examples)

`$string``$trim``nullify()``null``true``null``''``true``null``''``false``null``' '``true``null``' '``false``' '``'0'``true``'0'``'0'``false``'0'``' 0 '``true``'0'``' 0 '``false``' 0 '``' PHP '``true``'PHP'``' PHP '``false``' PHP '``' 🐘🎉 '``true``'🐘🎉'`Credits
-------

[](#credits)

- [Vic Cherubini](https://github.com/viccherubini), [1:N Labs, LLC](https://1tomany.com)

License
-------

[](#license)

The MIT License

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance100

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity43

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

Every ~0 days

Total

3

Last Release

1d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/60762?v=4)[Vic Cherubini](/maintainers/viccherubini)[@viccherubini](https://github.com/viccherubini)

---

Top Contributors

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

---

Tags

unicodestringsnullify

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/1tomany-nullify/health.svg)

```
[![Health](https://phpackages.com/badges/1tomany-nullify/health.svg)](https://phpackages.com/packages/1tomany-nullify)
```

###  Alternatives

[symfony/console

Eases the creation of beautiful and testable command line interfaces

9.8k1.2B14.9k](/packages/symfony-console)[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k18.3M415](/packages/easycorp-easyadmin-bundle)[flow-php/etl

PHP ETL - Extract Transform Load - Abstraction

378637.6k121](/packages/flow-php-etl)[api-platform/metadata

API Resource-oriented metadata attributes and factories

275.5M254](/packages/api-platform-metadata)[symfony/ai-bundle

Integration bundle for Symfony AI components

31798.1k29](/packages/symfony-ai-bundle)[heimrichhannot/contao-pwa-bundle

A bundle to provide progressive web app support for contao.

101.2k](/packages/heimrichhannot-contao-pwa-bundle)

PHPackages © 2026

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