views:

3985

answers:

6

I have a function isNotEmpty which returns true if the string is not empty and false if the string is empty. I've found out that it is not working if I pass an empty string through it.

function isNotEmpty($input) 
{
    $strTemp = $input;
    $strTemp = trim($strTemp);

    if(strTemp != '') //Also tried this "if(strlen($strTemp) > 0)"
    {
         return true;
    }

    return false;
}

The validation of the string using isNotEmpty is done:

if(isNotEmpty($userinput['phoneNumber']))
{
    //validate the phone number
}
else
{
    echo "Phone number not entered<br/>";
}

If the string is empty the else doesn't execute, I don't understand why, can someone please shed some light on this please.

A: 

maybe you can try this

if(isNotEmpty($userinput['phoneNumber']) == true)

that's because of the php configuration in php.ini

martani_net
A: 

I always use a regular expression for checking for an empty string, dating back to CGI/Perl days, and also with Javascript, so why not with PHP as well, e.g. (albeit untested)

return preg_match('/\S/', $input);

Where \S represents any non-whitespace character

George Jempty
+9  A: 

Simple problem actually. Change:

if(strTemp != '')

to

if($strTemp != '')

Arguably you may also want to change it to:

if($strTemp !== '')

since != '' will return true if you pass is numeric 0 and a few other cases due to PHP's automatic type conversion.

Also bear in mind PHP has an empty() function already.

cletus
Arguably, change the if into: return $strTemp !== '';
strager
Yes, that's it, I don't know how I missed on the $ sign. Also tested passing value 0 and it works. Thanks for your response.
bgosalci
you don't want to use empty(). Consider a string of spaces: $x = " "; var_dump(!empty($x)); /* (TRUE) */ var_dump(isNotEmpty($x)); /* (FALSE) */
nickf
The OP is trimming the string. In this case its appropriate.
cletus
+3  A: 

In your if clause in the function, you're referring to a variable 'strTemp' that doesn't exist. '$strTemp' does exist, thou.

But PHP already have an empty()-function available, why make your own?

if (empty(trim($str)))
    /* String is empty */
else
    /* Not empty */

From php.net:

Return Values

Returns FALSE if var has a non-empty and non-zero value.

The following things are considered to be empty:

* "" (an empty string)
* 0 (0 as an integer)
* "0" (0 as a string)
* NULL
* FALSE
* array() (an empty array)
* var $var; (a variable declared, but without a value in a class)

http://www.php.net/empty

Björn
Not all implementations would want "0" to be evaluated as empty. And if he wanted that, couldn't he just as well use the if($x) comparison i.e. If(!trim($str))?
Calvin
Note that doing $tmpStr != '' will also return true of $tmpStr holds 0 or false or another empty/false value.
Pim Jager
Your version won't actually work: empty works on variables, not on expressions.
Korbinian
A: 

PHP evaluates an empty string to false, so you can simply use:

if (trim($userinput['phoneNumber'])) {
  // validate the phone number
} else {
  echo "Phone number not entered<br/>";
}
troelskn
This works fine until you pas 0. Otherwise the idea is great, I'll us it it on some other places. Thanks for your response.
bgosalci
+1  A: 

Well, instead of an answer (I believe you fixed your problem already), I'll offer you a piece of advice.

I don't know about all the others, but I personally get very annoyed at the sight of something like:

if(<<condition>>)
    {
         return true;
    }

    return false;

this calls for an elegant "return (<<condition>>);" solution. Please always take a look at your code and remove this sort of logic. You don't need an IF statement for every situation.

Peter Perháč