Determining Empty variables - Printable Version -Shoutbox (https://shoutbox.menthix.net) +-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58) +--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4) +---- Forum: Scripting (/forumdisplay.php?fid=39) +----- Thread: Determining Empty variables (/showthread.php?tid=83434) Determining Empty variables by le-foot on 04-28-2008 at 10:36 AM
How do I determine if a variable is empty? RE: Determining Empty variables by Skarbo on 04-28-2008 at 10:41 AM
Some methods: RE: Determining Empty variables by matty on 04-28-2008 at 01:20 PM
code: Each variable has a "type" if the variable is empty it will be undefined. This checks if the veriable has been initialized as a specific type. Mattike's post is more correct RE: Determining Empty variables by Matti on 04-28-2008 at 04:14 PM
To make a function which acts exactly like the empty() function in PHP, you'll have to go a bit further, as stated in the PHP documentation: quote:Depending on what you want to do, you can choose which "kind of emptiness" you want to check:
RE: Determining Empty variables by vikke on 04-28-2008 at 04:41 PM Mattike: == is the equal operator, not ===. RE: Determining Empty variables by matty on 04-28-2008 at 05:55 PM
quote:=== Is the bitwise-equal operator. RE: Determining Empty variables by John Anderton on 04-28-2008 at 06:00 PM
quote:Also used to type specificity. vikke, in php if you evaluate 0==false, the value would be true. Because its not testing the type of operands stored. doing 0===false would make it test the type as well which as matty correctly pointed out, is done by checking equality bitwise RE: Determining Empty variables by MeEtc on 04-28-2008 at 10:10 PM
I recommend checking out the PHP.js project site, there are many PHP equivalents posted that are available RE: Determining Empty variables by Stigmata on 04-28-2008 at 10:49 PM
I normally use (!$variable)? RE: Determining Empty variables by Matti on 04-29-2008 at 04:30 PM
quote:It totally depends of what you want to do with it. If you're working with arrays which may be empty or strings which may equal "0", you're better off with empty(). In normal cases however, (!$variable) will do just fine. RE: RE: Determining Empty variables by vikke on 04-29-2008 at 05:05 PM
quote:Oh I'm sorry, I don't know scripting too well, I'm used to C where == is the bitwise-equal operator. RE: Determining Empty variables by WDZ on 04-29-2008 at 05:46 PM
quote:Why are we talking about PHP in a JScript thread? Anyway, these are equivalent with one major difference... if(!$variable) { } if(empty($variable)) { } If $variable has not been set, the first will generate a warning message (which you'll only see if your error_reporting level includes E_NOTICE). PHP Notice: Undefined variable: variable So if you're sure a variable has been set, just use !$variable. If not, use empty(). Note: empty() is just a shorter way of doing this... if(!isset($variable) || !$variable) { } |