PHPackages                             skollro/otherwise - 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. skollro/otherwise

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

skollro/otherwise
=================

Functional when-otherwise conditionals for PHP.

v1.2.0(8y ago)9161MITPHPCI failing

Since Mar 30Pushed 5y ago3 watchersCompare

[ Source](https://github.com/skollro/otherwise)[ Packagist](https://packagist.org/packages/skollro/otherwise)[ RSS](/packages/skollro-otherwise/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (5)Dependencies (1)Versions (7)Used By (0)

Functional when-otherwise conditionals
======================================

[](#functional-when-otherwise-conditionals)

[![Latest Version](https://camo.githubusercontent.com/ca8269a59e6e942fddba167bc0f7864f67b0dd44e11e8b9916f538dd228b2022/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f736b6f6c6c726f2f6f74686572776973652e7376673f7374796c653d666c61742d737175617265)](https://github.com/skollro/otherwise/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/a5f49706b99e905a1f6aab06c37b56b7faef01900de9d79725e07000aaa7967d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f736b6f6c6c726f2f6f74686572776973652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/skollro/otherwise)[![StyleCI](https://camo.githubusercontent.com/7ff87a31d9e222be80d69c71c408a0fba859eeb282bf8a714d5a116521a9c043/68747470733a2f2f7374796c6563692e696f2f7265706f732f3132373431303031372f736869656c64)](https://styleci.io/repos/127410017)

This package allows to replace PHP conditionals by an easy functional match-when-otherwise syntax.

```
$result = match([1, 2, 3])
    ->when(function ($value) {
        return in_array(2, $value);
    }, '2 was found')
    ->otherwise('2 was not found');

// $result is "2 was found"
```

Install
-------

[](#install)

You can install this package via composer:

```
composer require skollro/otherwise
```

Usage
-----

[](#usage)

Every conditional `match` consists out of one or multiple `when` and one `otherwise` to provide values for each path.

#### `match($value, ...$params): Match`

[](#matchvalue-params-match)

This package provides a helper function `match`. The first value is the value to match against. You can pass a variable amount of `$params` which are passed to every callable that resolves a `$result`.

```
use Skollro\Otherwise\Match;
use function Skollro\Otherwise\match;

$match = match($value);
$match = Match::value($value);
```

#### `when($condition, $result): Match`

[](#whencondition-result-match)

`$condition` is a bool, a callable or a value to compare against (using `==`). `$result` takes either some value or a callable for lazy evaluation. More specific conditions have to be defined first because the first match is the final result.

```
$result = match('A', 'Some value')
    ->when('B', function ($value) {
        return "{$value} is always false: A != B";
    })
    ->when(true, function ($value, $param) {
        return "This is always true ({$param})";
    })
    ->when(function ($value) {
        return strlen($value) == 1;
    }, 'This is not the first match')
    ->otherwise('B');

// $result is "This is always true (Some value)" because it's the first condition that evaluates to true
```

#### `whenInstanceOf($type, $result): Match`

[](#wheninstanceoftype-result-match)

This is just a shortcut method for `$value instanceof A`. `$type` is anything that can be on the left side of an `instanceof` operator. `$result` takes either some value or a callable for lazy evaluation. More specific conditions have to be defined first because the first match is the final result.

```
$result = match(new A)
    ->whenInstanceOf(B::class, 'This is false')
    ->whenInstanceOf(A::class, 'This is true')
    ->when(function ($value) {
        return $value instanceof A;
    }, 'This is not the first match')
    ->otherwise('C');

// $result is "This is true" because it's the first condition that evaluates to true
```

#### `whenThrow($condition, $result): Match`

[](#whenthrowcondition-result-match)

`$condition` is a bool, a callable or a value to compare against (using `==`). `$result` takes either an exception class name, an exception instance or a callable that returns an exception. More specific conditions have to be defined first because the first match throws the exception instantly.

```
$result = match('A')
    ->when(false, 'This is always false')
    ->whenThrow('A', Exception::class)
    ->otherwise('C');

// Exception is thrown
```

#### `otherwise($value)`

[](#otherwisevalue)

`$value` is of type callable or some value. Supplies the default value if no `when` has evaluated to `true` before.

```
$result = match('A')
    ->when(false, 'This is always false')
    ->otherwise('B');

// $result is "B"

$result = match('A', 'Some value')
    ->when(false, 'This is always false')
    ->otherwise(function ($value, $param) {
        return "{$value} ({$param})";
    });

// $result is "A (Some value)"

$result = match('A')
    ->when(false, 'This is always false')
    ->otherwise('strlen');

// $result is 1
```

#### `otherwiseThrow($value)`

[](#otherwisethrowvalue)

Throws an exception if no `when` has evaluated to `true` before. It takes an exception class name, an exception instance or a callable that returns an exception.

```
// recommended: an instance of the exception is only created if needed
$result = match('A')
    ->when(false, 'This is always false')
    ->otherwiseThrow(Exception::class);

$result = match('A', 'Some value')
    ->when(false, 'This is always false')
    ->otherwiseThrow(function ($value, $param) {
        throw new Exception("Message {$value} ({$param})");
    });

// not recommended
$result = match('A')
    ->when(false, 'This is always false')
    ->otherwiseThrow(new Exception);
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity67

Established project with proven stability

 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 ~6 days

Total

5

Last Release

2990d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8849605?v=4)[Simon Kollross](/maintainers/skollro)[@skollro](https://github.com/skollro)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/skollro-otherwise/health.svg)

```
[![Health](https://phpackages.com/badges/skollro-otherwise/health.svg)](https://phpackages.com/packages/skollro-otherwise)
```

###  Alternatives

[pingpong/generators

Laravel Generators

31239.0k8](/packages/pingpong-generators)[rezozero/social-links

Provide social network url for sharing.

155.9k1](/packages/rezozero-social-links)

PHPackages © 2026

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