PHPackages                             tiny-blocks/environment-variable - 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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. tiny-blocks/environment-variable

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

tiny-blocks/environment-variable
================================

Provides a type-safe environment variable reader for PHP, with strict integer and boolean conversion.

1.2.1(2mo ago)11.7k↓86.8%2MITPHPPHP ^8.5CI passing

Since Dec 9Pushed 2w ago1 watchersCompare

[ Source](https://github.com/tiny-blocks/environment-variable)[ Packagist](https://packagist.org/packages/tiny-blocks/environment-variable)[ Docs](https://github.com/tiny-blocks/environment-variable)[ RSS](/packages/tiny-blocks-environment-variable/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (5)Dependencies (10)Versions (6)Used By (2)

Environment variable
====================

[](#environment-variable)

[![License](https://camo.githubusercontent.com/f8df3091bbe1149f398a5369b2c39e896766f9f6efba3477c63e9b4aa940ef14/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e)](LICENSE)

- [Overview](#overview)
- [Installation](#installation)
- [How to use](#how-to-use)
- [License](#license)
- [Contributing](#contributing)

Overview
--------

[](#overview)

Provides a type-safe environment variable reader for PHP, wrapping raw values behind a typed accessor with explicit string, integer, and boolean conversion methods. Supports defaults for missing variables and distinguishes between absent and empty states. Built to surface configuration errors at read time rather than propagate silent coercions through the system.

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

[](#installation)

```
composer require tiny-blocks/environment-variable
```

How to use
----------

[](#how-to-use)

### Creating an environment variable

[](#creating-an-environment-variable)

To create and work with environment variables, use the `from` method to get an instance of the environment variable.

```
use TinyBlocks\EnvironmentVariable\EnvironmentVariable;

EnvironmentVariable::from(name: 'MY_VAR');
```

To retrieve an environment variable with the option of providing a default value in case the variable does not exist, use the `fromOrDefault` method.

If the environment variable is not found, the method returns an instance carrying the provided default value instead of throwing an exception.

```
use TinyBlocks\EnvironmentVariable\EnvironmentVariable;

EnvironmentVariable::fromOrDefault(name: 'MY_VAR', defaultValueIfNotFound: 'default_value');
```

### Conversions

[](#conversions)

Once you have an instance of the environment variable, you can convert its value into various types.

#### Convert to string

[](#convert-to-string)

To convert the environment variable to a string.

```
use TinyBlocks\EnvironmentVariable\EnvironmentVariable;

$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
$environmentVariable->toString();
```

#### Convert to integer

[](#convert-to-integer)

To convert the environment variable to an integer.

```
use TinyBlocks\EnvironmentVariable\EnvironmentVariable;

$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
$environmentVariable->toInteger();
```

#### Convert to boolean

[](#convert-to-boolean)

To convert the environment variable to a boolean.

```
use TinyBlocks\EnvironmentVariable\EnvironmentVariable;

$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
$environmentVariable->toBoolean();
```

### Check if the environment variable has a value

[](#check-if-the-environment-variable-has-a-value)

Checks if the environment variable has a value. Values like `false`, `0`, and `-1` are valid and non-empty.

```
use TinyBlocks\EnvironmentVariable\EnvironmentVariable;

$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
$environmentVariable->hasValue();
```

License
-------

[](#license)

Environment variable is licensed under [MIT](LICENSE).

Contributing
------------

[](#contributing)

Please follow the [contributing guidelines](https://github.com/tiny-blocks/tiny-blocks/blob/main/CONTRIBUTING.md) to contribute to the project.

###  Health Score

50

—

FairBetter than 95% of packages

Maintenance92

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 72.7% 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 ~124 days

Total

5

Last Release

73d ago

PHP version history (2 changes)1.0.0PHP ^8.3

1.2.0PHP ^8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/b00a83241d2af272255f1755bf6aff48e77821c59ef623520176d9e828977241?d=identicon)[gustavofreze](/maintainers/gustavofreze)

---

Top Contributors

[![gustavofreze](https://avatars.githubusercontent.com/u/22873481?v=4)](https://github.com/gustavofreze "gustavofreze (8 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")

---

Tags

envenvironmentenvironment-variableshacktoberfestopen-sourcephptiny-blocks

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tiny-blocks-environment-variable/health.svg)

```
[![Health](https://phpackages.com/badges/tiny-blocks-environment-variable/health.svg)](https://phpackages.com/packages/tiny-blocks-environment-variable)
```

###  Alternatives

[maclof/kubernetes-client

A simple yet elegant client for accessing and controlling a Kubernetes cluster.

237916.9k5](/packages/maclof-kubernetes-client)

PHPackages © 2026

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