Shoutbox

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?

For example, in PHP it is empty($variable), what is it in the script?

Thanks


RE: Determining Empty variables by Skarbo on 04-28-2008 at 10:41 AM

Some methods:
($variable == null)
or just
(!$variable)


RE: Determining Empty variables by matty on 04-28-2008 at 01:20 PM

code:
var myVariable;

if ( typeof myVariable === 'undefined' ) Debug.Trace ( 'Your variable is empty' );
else Debug.Trace( typeof myVariable );

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:
Source: http://www.php.net/manual/en/function.empty.php

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)

Depending on what you want to do, you can choose which "kind of emptiness" you want to check:
  • If it's a number, boolean, undefined or NULL variable, you can simply use if(!variable)
  • If it's a string, you can compare it to an empty string with if(variable === "")
  • If it's an array, you can check if it's empty with if(variable.length === 0)
  • etc...

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:
Originally posted by vikke
Mattike: == is the equal operator, not ===.
=== Is the bitwise-equal operator.
RE: Determining Empty variables by John Anderton on 04-28-2008 at 06:00 PM

quote:
Originally posted by matty
quote:
Originally posted by vikke
Mattike: == is the equal operator, not ===.
=== Is the bitwise-equal operator.
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

http://kevin.vanzonneveld.net/techblog/article/ja...nt_for_phps_empty/


RE: Determining Empty variables by Stigmata on 04-28-2008 at 10:49 PM

I normally use (!$variable)?

or is this bad practice?


RE: Determining Empty variables by Matti on 04-29-2008 at 04:30 PM

quote:
Originally posted by Stigmata
I normally use (!$variable)?

or is this bad practice?
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:
Originally posted by John Anderton
quote:
Originally posted by matty
quote:
Originally posted by vikke
Mattike: == is the equal operator, not ===.
=== Is the bitwise-equal operator.
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 :-)
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:
Originally posted by Stigmata
I normally use (!$variable)?
Why are we talking about PHP in a JScript thread? :p

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) { }