2

I have the following PHP code:

$required_fields = array ('menu_name','visible','position');
foreach($required_fields as $fieldname)
{
    if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) )
    {
        $errors [] = $fieldname;
    }
}

menu_name, visible and position are variables that are received through the post method.

When the value of visible is zero, it creates an entry into the error array.

What is the best way to detect if a variable is empty when 0 is considered "not empty"?

1

4 Answers 4

8

From PHP's manual:

empty() returns FALSE if var has a non-empty and non-zero value.

Do something like this:

if ( !IsSet ( $_POST['field'] ) || Trim ( $_POST['field'] ) == '' )

this will ensure that the field is set and that it does not contain a empty string

In essence: it is the empty() that is causing your problems not IsSet()

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

2 Comments

It's not a taste. In theory it's wrong, since the function isn't called that.
Hope PHP will begin case sensitive :P
1

Since user data is sloppy, I use a custom function that treats empty spaces as non data. It sounds like this will do exactly what you want. This function will consider "0" to be valid (aka non-empty) data.

function isNullOrEmpty( $arg )
{
    if ( !is_array( $arg ) )
    {
        $arg = array( $arg );
    }

    foreach ( $arg as $key => $value )
    {
        $value = trim($value);
        if( $value == "" || $value == null )
        {
            return true;
        }
    }
    return false;
}

Please note it supports arrays too but requires that each value in the array contains data, which can be useful as you can just do something like this:

$required = array( $_POST['name'],  $_POST['age'], $_POST['weight'] );
if ( isNullOrEmpty($required) )
{
  // required data is missing
}

PS: keep in mind this function will fire off PHP warnings if the value isn't set and there's no easy way around that, but you should NOT have warnings enabled in production anyways.

2 Comments

You could improve your code if you check wether the parameter is an array or not (I know you do this already). If it is not an array, just do $arg = array($arg) then you can use the foreach loop and you have to write the trim and comparison part only once which is better to maintain. Imagine you have other criteria to check, you would have to add them twice now (once for arrays and once for non-array values)(which is not DRY).
This is what I used in reference to your answer if ($_POST[$fieldname] == '' || !isset($_POST[$fieldname]))
0

If you want to assure an array key is present you can use array_key_exists() instead of empty()

The check will become a concatenation of is_array() and array_key_exists(), being paranoid of course

1 Comment

I thought about that too, but isset() also returns false if the key is present but contains a null value ( where array_key_exists() returns true). So in this use case I really think isset() fits better.
-1

Can't you just add another line with something like:

if (!isset($_POST[$fieldname]) || empty($_POST[$fieldname]) )
{
    if ($fieldname != 'visible' || $_POST[$fieldname] != 0)
    { 
        $errors [] = $fieldname;
    }
} 

1 Comment

You will get a notice if !isset($_POST[$fieldname]) is true and $fieldname != 'visible' is false as you then try to access $_POST[$fieldname].

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.