1

I want to validate a field which is required. I am using following condition to check whether the field is not empty:

isset($value) && !empty($value)

But if I write 0 in the fields, it will still say the field is required. The empty() function considers 0 as an empty value as told in PHP manual but what else should I do? I have tried writing many conditions but they didn't work as expected.

I saw user contributions and found:

empty($var) && !is_bool($var)

I tried adding it to my condition like this but didn't work:

isset($value) && !empty($value) && is_bool($value)
8
  • What's actually saved in the $value when you var_dump it? Commented Aug 5, 2014 at 14:39
  • Of what type is the variable you are trying to validate? Commented Aug 5, 2014 at 14:40
  • @ParagTyagi I'm making my own premium validation class. There isn't one value, I'm writing a single condition for integer type values and string type values. Commented Aug 5, 2014 at 14:42
  • Just a sidenote, this expression is redundant: isset($value) && !empty($value). empty() also checks if the variable is set, so there's no need to do a isset() check. Commented Aug 5, 2014 at 14:43
  • @VladCazacu isset checks whether variable is set & is not null while empty checks that variables is not null. Commented Aug 5, 2014 at 14:45

9 Answers 9

1

Try below code:

$value = 0;

if(isset($value) && $value !== "")
{
    echo "Field not required";
}
else
{
    echo "Field required";
}


Output:

Field not required


Demo:

http://3v4l.org/JWJbj


Explanation:

The integer 0 is truthy. It means that anything that checks it in a boolean'ish manner will treat it as false. In particular, !empty($value) will evaluate to false, so is_numeric would never even get a chance to fail.

Short version: empty is too wishy-washy in this case. Check for null and '' explicitly (with data-type hence used !==).

Sign up to request clarification or add additional context in comments.

Comments

0

You can't check for empty. Basically you can only stick to the isset test.

If a field in the form was not filled this variable field will not be inside the $_POST superglobal and isset will return false. If the user typed 0 the variable name will exist inside $_POST isset will return true.

Comments

0

I would do it as following:

function emptyTextValue($value)
{
    return trim((string) $value) !== '';
}

Comments

0

You know that all you get is a string, so you don't need full functionality of empty() function:

isset($_POST['yourFieldName']) && $_POST['yourFieldName'] !== ''

1 Comment

Be wary of whitespaces.
0

You can replace the empty function by :

$value !== ""

If it's not exactly empty, then it will validate the condition.

Comments

0

What I've done for my test is convert the empty check to a strlen check. Because PHP is so friendly, you can run a string comparison on the value. A value of null will return a 0 length string, while a value of 0 will return a length of 1. So what I've done is this:

if(isset($val) && strlen($val) > 0){
    // Do stuff
}

Comments

0

Try

isset($value) && $value != ''

Comments

0

I have found a nice condition by myself.

$value === 0 || $value === "0" ? isset($value) : isset($value) && !empty($value)

This works perfectly for every case.

Comments

0

I would check to be sure that the form had been submitted and then trim the field 'msg' in case there were any white space chars, i.e. newlines, carriage returns, tabs, spaces and then test that result to make sure it's not the empty string as follows:

<?php
if ( isset($_POST['msg']) && trim($_POST['msg']) !== '') {

    // other code

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.