Why a function checking if a string is empty always returns true? [closed] Why a function checking if a string is empty always returns true? [closed] php php

Why a function checking if a string is empty always returns true? [closed]


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.

You should not use the built-in empty() function for this; see comments and the PHP type comparison tables.


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


PHP have a built in function called empty()the test is done by typing if(empty($string)){...}Reference php.net : php empty