How to make Empty() accept the value 0?

Asked

Viewed 868 times

7

I have a registry where I check if the fields form filled in, in some of the fields you can only put the digits from 0 to 9.

But if the person puts 0’s as if she had not filled anything.

This is provided by the empty(). A small section of the check is there.

}else if (empty($idade)){
  • Can do }else if (empty($idade) || $idade == 0){. But explain better than types of value $idade may have we can be clearer in helping...

  • age can only have two digits. Ex: 34. But I want 0 to also be accepted.

  • If you use (empty($idade) || $idade == 0) There will be problems, you are checking if the age is empty or if the age is equal to zero. His problem I believe occurs when age is a valid age, but he recognizes it as empty by being zero.

  • That’s right @Pedro Henrique

  • 1

    @Ivanveloso, you wrote in the comments: "But I want 0 to be accepted as well." This is what you want?

4 answers

7


You can improve the expression using something like:

else if (empty($idade) && $idade != 0)

This will check if the $age is empty and is not zero. But it is better to consider a more specific validation as the is_int of PHP.

  • Ivan wrote: Porém quero que o 0 também seja aceito

  • 2

    The excerpt "Else if (Empty($age) && $age != 0)" did not resolve, but replaces Empty with is_int. Ai solved.

  • @Sergio empty(0) is true.

  • @bfavaretto, hmm... truth, my mistake. In this case my answer is wrong and the question makes even less sense... :/

  • It’s not wrong at all empty(0) is true, but 0!=0 is false, the condition is false, that is, does not enter into the verification of the Empty when the age is 0.

2

I’m late, but I hope I can help someone:

I bumped into a case where it is necessary to check an index of a array, using the other examples, you may get the error Undefined Index. Generally use the empty(), to test values in arrays because he doesn’t give a warning if the index does not exist.

In the case 0 is a considered value empty and if you try to access an index of a array which does not exist, the warning previously mentioned will be released.

So I arrived at the following solution:

if (empty($array['age']) && @$array['age'] !== 0) {
    throw new RuntimeException('Idade não informada.', 400);
}

Note that I used the operator @, that suppresses the warinings on that line, that is NOT a recommended practice, but that in this case, only a simple and isolated part of the code is affected and will not bring problems.

In place of @$array['age'] !== 0, you might think about using isset($array['age']), but the isset considers the value in $array['age'] = null as false.

It is a pity that in PHP still by default gives this type of problem (for example Javascript allows access to indexes not defined), but that can be changed in the settings, the big problem is not treat the index before performing some action, how to access the method of an undefined index, so always be careful with this type of data manipulation, which can generate unexpected bugs in the application.

1

From what I understand, you want to check if the value is empty with the empty, but wants to accept it if it is 0.

in case it would have to be checked first if the value is equal to 0, since the check will occur from right to left; otherwise, Empty would return true when checking first "0" as an empty value.

I’d do it this way:

0

Another form of solution would be:

else if (empty($idade) || count(ltrim($idade," ")) < 1)

What this check does is:

  • Checks whether the variable $idade is empty;
  • Or if its length is less than 1 character (not counting spaces)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.