https://github.com/pixelbrackets/not-empty
not empty() - Adds methods to determine whether a variable is blank or present - Inspired by Ruby on Rails
https://github.com/pixelbrackets/not-empty
blank empty php
Last synced: about 1 month ago
JSON representation
not empty() - Adds methods to determine whether a variable is blank or present - Inspired by Ruby on Rails
- Host: GitHub
- URL: https://github.com/pixelbrackets/not-empty
- Owner: pixelbrackets
- Created: 2020-08-05T13:13:20.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-12-06T13:53:06.000Z (about 4 years ago)
- Last Synced: 2025-03-06T18:29:39.045Z (11 months ago)
- Topics: blank, empty, php
- Language: PHP
- Homepage: https://gitlab.com/pixelbrackets/not-empty/
- Size: 9.77 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# not empty()
[](https://packagist.org/packages/pixelbrackets/not-empty/)
[](https://gitlab.com/pixelbrackets/not-empty/pipelines)
[](https://gitlab.com/pixelbrackets/not-empty#requirements)
[](https://spdx.org/licenses/GPL-2.0-or-later.html)
Add methods to determine whether a variable is blank or present.
Inspired by Ruby on Rails.
## Idea
This package is a hackathon product. Idea was to port the
[Ruby on Rails methods](https://blog.appsignal.com/2018/09/11/differences-between-nil-empty-blank-and-present.html)
`blank` and `present` to PHP.
PHP has the [empty](https://www.php.net/manual/en/function.empty.php) method,
but more often I need to check whether a variable is not empty.
That's why a `notEmpty` method is available as well.
See the list below to compare the different behaviour of these methods.
```
┌─────────────────────────┬─────────┬────────────┬─────────┬───────────┐
│ VALUE │ EMPTY() │ NOTEMPTY() │ BLANK() │ PRESENT() │
├─────────────────────────┼─────────┼────────────┼─────────┼───────────┤
│ string '' │ true │ │ true │ │
│ string 'acme' │ │ true │ │ true │
│ string ' ' │ │ true │ true │ │
│ string ' ' │ │ true │ true │ │
│ string "\t\n" │ │ true │ true │ │
│ int 0 │ true │ │ true │ │
│ float 0.0 │ true │ │ true │ │
│ int 42 │ │ true │ │ true │
│ float 3.14 │ │ true │ │ true │
│ string '0' │ true │ │ true │ │
│ string '1337' │ │ true │ │ true │
│ null │ true │ │ true │ │
│ bool true │ │ true │ │ true │
│ bool false │ true │ │ true │ │
│ array [] │ true │ │ true │ │
│ array ['acme'] │ │ true │ │ true │
│ object {} │ │ true │ true │ │
│ object {"foo" => "bar"} │ │ true │ │ true │
└─────────────────────────┴─────────┴────────────┴─────────┴───────────┘
```
## Requirements
- PHP
## Installation
Packagist Entry https://packagist.org/packages/pixelbrackets/not-empty/
## Source
https://gitlab.com/pixelbrackets/not-empty/
Mirror https://github.com/pixelbrackets/not-empty/
## Usage
See [tests/demo.php](./tests/demo.php).
```php
use Pixelbrackets\NotEmpty\Blank;
use Pixelbrackets\NotEmpty\NotEmpty;
use Pixelbrackets\NotEmpty\Present;
# Is a string empty? → Use PHPs »empty«
if (empty('')) { … }
# Is a string not empty? → Use »empty() === false«
if (empty('acme') === false) { … }
# → …or use »notEmpty« instead
if (NotEmpty::notEmpty('acme')) { … }
# A strings with whitespaces is blank → use a combination of conditions
if (is_string(' ') && empty(trim(' '))) { … }
# → …or use »blank« instead
if (Blank::blank(' ')) { … }
# → use »present« to check the opposite of »blank«
if (Present::present('acme')) { … }
```
💡 If you want to use the methods as global functions (`blank()` instead of
the namespaced `Pixelbrackets\NotEmpty\Blank::blank()`) then install the
»global« branch, using `composer require pixelbrackets/not-empty:dev-global`.
## License
GNU General Public License version 2 or later
The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html.
## Author
Dan Untenzu ( / [@pixelbrackets](https://pixelbrackets.de))
## Changelog
See [./CHANGELOG.md](CHANGELOG.md)
## Contribution
This script is Open Source, so please use, patch, extend or fork it.
This package is not in active delopment, contributions are welcome though.