Home > Back-end >  PHP how to check for an empty field?
PHP how to check for an empty field?

Time:02-24

As per PHP manual empty function check for this type of cases,

$var = '';
$var = 0;
$var = 0.0;
$var = FALSE;
$var = NULL;

But I came across this if $var = 0.00 or .0 or ' '(space) it is not empty, how can I properly check if a field is empty?

CodePudding user response:

If your definition of "empty" isn't the same as PHP's then you'll need to implement a function which makes additional checks for the extra values you consider to consider to represent "empty", as well as the ones which empty() already does. i.e. the way to check if it's empty as per your definition, is to check for each of the values you want to check for. Simple as that :-)

Note: 0.00 and .0 already cause empty() to return true (demo: https://3v4l.org/v1HNb) so we can ignore those. If you're thinking of the string represenation of those though (i.e. "0.00" and ".0") then your new function could take those into account.

For example:

function extraEmpty($var)
{
  return (empty($var) || $var === "0.00" || $var === ".0" || $var === " ");
}

Demo: https://3v4l.org/Ri8c3

N.B. I would argue that since " " isn't an empty string (i.e. it contains a space which is a perfectly valid character), then it shouldn't be classed as representing "empty", but that's up to you. For consideration: what about a string containing nothing except 2 space characters? or 3? Or 20? or 2000? What do you do with those? You risk opening a can of worms, IMHO.

CodePudding user response:

This can be option for the requirement:

if(empty($var) && !is_float($var)){
  // do whatever
}

CodePudding user response:

The PHP function empty() would be the right function. But a white space will not triggert. then you have trim before you check.

<?php
$var1 = '';
$var2 = 0;
$var3 = 0.0;
$var4 = FALSE;
$var5 = NULL;

echo '#1 -' . empty($var1). ' | '; // true
echo '#2 -' . empty($var2). ' | '; // true
echo '#3 -' . empty($var3). ' | '; // true
echo '#4 -' . empty($var4). ' | '; // true
echo '#5 -' . empty($var5). ' |'; // true

CodePudding user response:

//This might help you using empty() function to check your variable.

$var = "";
//empty($var)=true

$var = null;
//empty($var)=true

var $var;
//empty($var)=true

$var = "undefined"; //empty($var)=true

$var = array();
//empty($var)=true

$var = false;
//empty($var)=true

$var = true;
//empty($var)=false

$var = 1;
//empty($var)=false

$var = 42;
//empty($var)=false

$var = 0;
//empty($var)=true

$var = -1;
//empty($var)=false

$var = "1";
//empty($var)=false

$var = "0";
//empty($var)=true

$var = "-1";
//empty($var)=false

$var = "php";
//empty($var)=false

$var = "true";
//empty($var)=false

$var = "false";
//empty($var)=false

CodePudding user response:

I ended up making this function for my custom empty check

function fn_check_empty($data){
    if(!$data || !is_array($data)){
        return false;
    }
    foreach($data as $details){
        foreach ($details as $key => $value) {
            $var = str_replace(' ','', $value);
            $var = str_replace('.','',$var);
            if(!$var){
                return false;
            }
        }
    }
    return true;
}

Here $data will have array something like this

array(
    0 => array(
        0 => "0.00",
        1 => "   0.00",
        2 => ".0   "
    );
);
  •  Tags:  
  • php
  • Related