A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2009 yearPHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate] - page 1
User InfoPosts
PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#1
Possible Duplicate:
Fixing the PHP empty function




In PHP, empty() is a great shortcut because it allows you to check whether a variable is defined AND not empty at the same time.

What would you use when you don(t want "0" (as a string) to be considered empty, but you still want false, null, 0 and "" treated as empty?

That is, I(m just wondering if you have your own shortcut for this:

if (isset($myvariable) && $myvariable != "") ;// do something
if (isset($othervar ) && $othervar != "") ;// do something
if (isset($anothervar) && $anothervar != "") ;// do something
// and so on, and so on


I don(t think I can define a helper function for this, since the variable could be undefined (and therefore couldn(t be passed as parameter).

posted date: 2009-04-08 21:53:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2009-04-08 21:53:01


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#3
if ((isset($var) && $var === "0") || !empty($var)){}This way you will enter the if-construct if the variable is set AND is "0", OR the variable is set AND not = null ("0",null,false)

posted date: 2009-04-08 22:27:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#4
This should do what you want:function notempty($var) { return ($var==="0"||$var);}Edit: I guess tables only work in the preview, not in actual answer submissions. So please refer to the PHP type comparison tables for more info.notempty("") : falsenotempty(null) : falsenotempty(undefined): falsenotempty(array()) : falsenotempty(false) : falsenotempty(true) : truenotempty(1) : truenotempty(0) : falsenotempty(-1) : truenotempty("1") : truenotempty("0") : truenotempty("php") : trueBasically, notempty() is the same as !empty() for all values except for "0", for which it returns true.Edit: If you are using error_reporting(E_ALL), you will not be able to pass an undefined variable to custom functions by value. And as mercator points out, you should always use E_ALL to conform to best practices. This link (comment #11) he provides discusses why you shouldn(t use any form of error suppression for performance and maintainability/debugging reasons.See orlandu63(s answer for how to have arguments passed to a custom function by reference.

posted date: 2009-04-08 23:26:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#5
That will fail when the variable is undefined.

posted date: 2009-04-08 23:41:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#6
How do you mean? If passed an undefined variable, the function returns false--which is the opposite of what empty() returns: us2.php.net/manual/en/types.comparisons.php That is the desired behavior.

posted date: 2009-04-09 01:57:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#7
if(isset($var) && ($var === (0( || !empty($var))){}

posted date: 2009-04-09 03:20:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#8
It does not fail, but produces a warning. So, although it does work, it is not The Right Way(tm). You should use isset() before checking the variable with ===

posted date: 2009-04-09 08:45:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#9
Strange, it doesn't produce any warnings when I have error reporting set to E_STRICT. If does however produce an error when I use E_ALL. But then using isset() inside of the function does not prevent the error either.

posted date: 2009-04-09 13:52:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#10
function Void($var){ if (empty($var) === true) { if (($var === 0) || ($var === (0()) { return false; } return true; } return false;}

posted date: 2009-04-09 19:17:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#11
E_STRICT isn't a superset of E_ALL: php.net/manual/en/errorfunc.constants.php. You should use (E_ALL | E_STRICT). You could use the @ operator, but that adds overhead (see smashingmagazine.com/2009/03/24/10 "tip" #9 and comment #11) and isn't The Right Way™ either.

posted date: 2009-04-10 04:00:00


Re: PHP: what's an alternative to empty(), where string “0” is not treated as empty? [duplicate]#12
This looks quite correct; it isn't really a shortcut for if (isset($myvariable) && $myvariable != "") though as it's longer. Thanks for the answer.

posted date: 2009-04-11 05:57:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.