https://github.com/lemmon/clsx-php
https://github.com/lemmon/clsx-php
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lemmon/clsx-php
- Owner: lemmon
- Created: 2025-07-20T06:07:36.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-23T07:14:44.000Z (12 months ago)
- Last Synced: 2025-07-23T08:10:40.283Z (12 months ago)
- Language: PHP
- Size: 2.93 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# clsx.php
> A tiny PHP utility to conditionally build `class` attribute strings.
> Inspired by [clsx for JavaScript](https://github.com/lukeed/clsx), it lets you easily compose class names using strings, arrays, and associative arrays.
---
## Installation
Install via Composer:
```bash
composer require lemmon/clsx
```
---
## Usage
```php
echo clsx('foo', true && 'bar', 'baz');
// Output: 'foo bar baz'
echo clsx(['foo', 0, false, 'bar']);
// Output: 'foo bar'
echo clsx(['foo'], ['', 0, false, 'bar'], [['baz', [['hello'], 'there']]], 'bye');
// Output: 'foo bar baz hello there bye'
echo clsx(['foo' => true, 'bar' => false, 'baz' => isSomethingTrue()]);
// Output: 'foo baz'
echo clsx(['foo' => true], ['--special' => 'active']);
// Output: 'foo --special'
```
---
## API
### `clsx(...$args): string`
Returns a space-separated string of valid class names.
#### Parameters
- `...$args`: mixed
Accepts any number of arguments. Arguments can be:
- Strings
- Arrays (flat or nested)
- Associative arrays (keys are class names, values are booleans or strings)
> **Note:** Falsy values like `false`, `null`, `0`, `''`, etc. are discarded.
> Only truthy values or valid strings are included in the final result.
#### Example
```php
clsx(true, false, '', null, 0);
// Output: ''
```
---
## License
MIT © [Jakub Pelák](https://www.jakubpelak.com/)