Handling half-truths in PHP

One drawback of using a dynamically typed language is that it makes code messier when trying to check for the “truthiness” and, or “falseness” of things.

When you intend to write clean, expressive code, you’d rather avoid cluttering your codebase with clumsy conditionals. In fact, you might even want to have as little conditionals as possible.

Introducting the Falsy package

This is why I wrote ‘Falsy’, a tiny package containing 2 functions to accomplish one mission: audit a set variables as being true or false.

There are many ways to simplify your conditional expressions, and combining these techniques with the falsy helper can save a lot of time and headaches.

In short

  • Tiny library (< 200 lines of code)
  • Small footprint (adds 2 functions to the global namespace)
  • No dependencies
  • Fully tested
  • Requires PHP version ^7.0

It is short (less than 200 lines of code), relies on no external library, and was built following test-driven development principles, which makes it very reliable.

It is available on Packagist and you can browse the code on Github.

Installation

composer require angle/falsy

Functions

function falsy(…$vars) : bool

If any of the given variables are truthy, the function will return false (returns true only when everything is falsy).

function truthy(…$vars) : bool

If any of the given variables are falsy, the function will return false (returns true only when everything is truthy).

Examples

If anything is falsy, the function returns true.

falsy(true, false, null); // true
truthy(true, false, null); // false

If all variables are truthy, the function returns false.

falsy(true, 1, [1]); // false
truthy(true, 1, [1]); // true

Null strings and empty arrays are falsy.

falsy('0', '', [], ['']); // true
truthy('0', '', [], ['']); // false

Arrays are recursively audited.

$array = [
    'foo' => false,
    'bar'=> [
        'baz' => false
    ]
];

falsy($array); // true
truthy($array); // false

$array = [
    'foo' => true,
    'bar'=> [
        'baz' => 1
    ]
];

falsy($array); // false
truthy($array); // true

Objects are audited on their properties.

$object = new stdClass;

falsy($object); // true
truthy($object); // false

$object->foo = false;
$object->bar = null;
$object->baz = 0;

falsy($object); // true
truthy($object); // false

$object->foo = true;
$object->bar = 'string';
$object->baz = 1;

falsy($object); // false
truthy($object); // true

Closures are audited on their return value.

$void = function () { return; };

falsy($void); // true
truthy($void); // false

$false = function () { return false; };

falsy($false); // true
truthy($false); // false

$string = function () { return 'string'; };

falsy($string); // false
truthy($string); // true

falsy($void, $false, $string); // true
truthy($void, $false, $string); // false

Finally, you may pass as many parameters as you need.

falsy(
    false,
    null,
    0,
    0.0,
    '0',
    [],
    [''], // Php would consider this as being true, but really, is it? No.
    ['' => ''],
    [false, null],
    ['' => '', 0 => ['key' => null, 'foo' => [''], 'empty' => []]], // Array keys are ignored
    new stdClass, // Objects with empty attributes are falsy
    function () { return; }, // Void closures are falsy
    function () { return false; },
    function () { return null; },
    function () { return ''; },
    function () { return 0; },
    function () { return []; },
    function () { return ['']; }
); // true
$object = new stdClass;
$object->foo = 'bar';

truthy(
    true,
    1,
    0.1,
    '1',
    ['foo'],
    ['foo' => 'bar'],
    [true],
    $object,
    function () { return true; },
    function () { return 1; },
    function () { return 0.1; },
    function () { return '1'; },
    function () { return ['foo']; },
    function () { return ['foo' => 'bar']; },
    function () { return [true]; },
    function () use ($object) { return $object; }
); // true

These exemples are part of the Falsy test suite.

Contributing

Contributions are welcomed! Feel free to submit a pull request anytime.

Licence

MIT License

Published by Ben Villiere

Software Developer since 2007. I enjoy solving complex problems with efficient code. My mission is to help people improve their professional life by automating mundane tasks. This blog is my technical journal where I write about my work.

Follow me on: | |