Skip to content

Instantly share code, notes, and snippets.

@mcaskill
Last active September 10, 2018 01:58
Show Gist options
  • Save mcaskill/45c02c7366ca7ccc94ec91ad286c6de7 to your computer and use it in GitHub Desktop.
Save mcaskill/45c02c7366ca7ccc94ec91ad286c6de7 to your computer and use it in GitHub Desktop.
PHP : Determine whether a variable has a non-empty value.

is_blank

(PHP 5 >= 5.4) is_blank — Determine whether a variable has a non-empty value.

Description

boolean is_blank( mixed $var )

Alternative to empty() that accepts non-empty values:

  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)

Parameters

  • var — The value to be checked.

Return Values

Returns FALSE if var exists and has a non-empty value. Otherwise returns TRUE.

Installation

With Composer

$ composer require mcaskill/php-is-blank

Without Composer

Why are you not using composer? Download Function.Is-Blank.php from the gist and save the file into your project path somewhere.

{
"name": "mcaskill/php-is-blank",
"description": "Determine whether a variable has a non-empty value.",
"license": "MIT",
"authors": [
{
"name": "Chauncey McAskill",
"email": "[email protected]",
"homepage": "https://github.com/mcaskill"
}
],
"keywords": [
"function"
],
"extra": {
"branch-alias": {
"dev-master": "1.x-dev"
}
},
"require": {
"php": ">=5.4.0"
},
"autoload": {
"files": ["Function.Is-Blank.php"]
}
}
<?php
if (!function_exists('is_blank')) {
/**
* Determine whether a variable has a non-empty value.
*
* Alternative to {@see empty()} that accepts non-empty values:
* - _0_ (0 as an integer)
* - _0.0_ (0 as a float)
* - _"0"_ (0 as a string)
*
* @param mixed $var The value to be checked.
* @return boolean Returns FALSE if var exists and has a non-empty value. Otherwise returns TRUE.
*/
function is_blank($var)
{
return empty($var) && !is_numeric($var);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment