cl
A simple, fast, and beautiful console logger for PHP. Designed for developers who use the built-in PHP server and need a lightweight, unobtrusive debugging tool without the overhead of larger solutions.
Currently accepting new projects and available for hire.
When building dynamic web applications in PHP, constructing the class
attribute for HTML elements can quickly become cumbersome. Developers often resort to a messy mix of string concatenation, ternary operators, and if
statements directly within the HTML, leading to code that is hard to read and prone to errors.
This led me to create clsx.php
, a PHP port of the incredibly popular JavaScript library clsx. This lightweight and dependency-free PHP function provides an elegant and efficient way to construct CSS class strings conditionally, making your code cleaner, more readable, and easier to maintain.
The core of the library is a single function, clsx()
, which accepts a variety of argument types. You can mix strings, arrays of strings, and associative arrays for conditional logic. The function intelligently processes them, filters out any "falsy" values, and returns a perfectly formatted, space-separated class string.
Here's a quick look at how it works:
// Arrays with falsy values
echo clsx(['foo', 0, false, 'bar']);
// -> 'foo bar'
// Associative arrays for conditions
echo clsx(['foo' => true, 'bar' => false, 'baz' => isSomethingTrue()]);
// -> 'foo baz'
// Mix and match nested arrays
echo clsx(['foo'], ['bar', [['baz']]]);
// -> 'foo bar baz'
// Kitchen sink - combining all types
echo clsx('base-class', ['active' => $isActive], null, ['btn', 'btn-primary'], false);
// -> 'base-class active btn btn-primary' (when $isActive is true)
Key features include an intuitive API, flexible arguments, conditional logic support, automatic falsy value filtering, nested array support, deduplication, and zero dependencies.
For me, clsx.php
has become an indispensable part of my PHP development process. It brings the elegance and simplicity of the JavaScript clsx library to PHP, and I'm excited to share it with the open-source community.
If you're looking for a clean and efficient way to handle CSS class construction in your PHP projects, I encourage you to give clsx.php
a try. You can find it on GitHub and Packagist.
A simple, fast, and beautiful console logger for PHP. Designed for developers who use the built-in PHP server and need a lightweight, unobtrusive debugging tool without the overhead of larger solutions.
An open-source demo blog application showcasing the Tablo theme built with SvelteKit. Features Markdown processing with mdsvex, Tailwind CSS styling, and modern web development best practices for creating fast, beautiful blogs.