26b / joiner
Join strings with conditional values
1.0.0
2026-10-03 20:44 UTC
Requires
- php: >=8.3
Requires (Dev)
- pestphp/pest: ^4.0 || ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Join strings with conditions attached to values or array keys.
Installation
composer require 26b/joiner
Requires PHP 8.3 or later.
Usage
<?php use function Joiner\join; $name = 'Ada'; $isReturning = true; $message = join(['Hello,', $name, ['welcome back' => $isReturning]], ' '); // 'Hello, Ada welcome back' join(args: ['first', 'second'], separator: ' / '); // 'first / second'
Arguments
- Pass all values in the first array argument; use the optional second argument to choose the separator (default:
''). - Non-empty strings and integers are included as strings, including
'0'. - Objects implementing
__toString()are included using their string value. - List arrays are recursively processed; associative arrays and
stdClassobjects include keys/properties whose values pass their condition. - Other non-string values in lists are ignored. For map conditions, non-empty strings are true; other values use PHP truthiness.
Other objects are ignored; map objects must be stdClass.
CSS class names
classnames(...$arguments) joins values with spaces like join(), then emits a PHP E_USER_WARNING for each output token that is not a CSS <ident> according to MDN. It returns the joined string unchanged. Use join() when the strings are not CSS class names.
use function Joiner\classnames; classnames('button', ['button-active' => true]); // 'button button-active'