PHPackages                             lemmon/clsx - 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. lemmon/clsx

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

lemmon/clsx
===========

A tiny PHP utility to conditionally build `class` attribute strings, inspired by clsx for JavaScript.

1.0.0(10mo ago)066MITPHPPHP &gt;=7.4

Since Jul 20Pushed 10mo agoCompare

[ Source](https://github.com/lemmon/clsx-php)[ Packagist](https://packagist.org/packages/lemmon/clsx)[ Docs](https://github.com/lemmon/clsx-php)[ RSS](/packages/lemmon-clsx/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (2)Used By (0)

clsx.php
========

[](#clsxphp)

> 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
------------

[](#installation)

Install via Composer:

```
composer require lemmon/clsx
```

---

Usage
-----

[](#usage)

```
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
---

[](#api)

### `clsx(...$args): string`

[](#clsxargs-string)

Returns a space-separated string of valid class names.

#### Parameters

[](#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

[](#example)

```
clsx(true, false, '', null, 0);
// Output: ''
```

---

License
-------

[](#license)

MIT © [Jakub Pelák](https://www.jakubpelak.com/)

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance55

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

303d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/69d2fc0d66f2e8e99490f3781a67d2af0d6ca9cb3e48db4a6199336344d4310c?d=identicon)[lemmon](/maintainers/lemmon)

---

Top Contributors

[![lemmon](https://avatars.githubusercontent.com/u/251591?v=4)](https://github.com/lemmon "lemmon (1 commits)")

---

Tags

phputilitycssclassclsx

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lemmon-clsx/health.svg)

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

###  Alternatives

[tailwindphp/tailwindphp

A full port of TailwindCSS 4.x to PHP

171.7k1](/packages/tailwindphp-tailwindphp)

PHPackages © 2026

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