Determining Empty variables |
Author: |
Message: |
le-foot
New Member
Posts: 9
Joined: Apr 2008
|
O.P. Determining Empty variables
How do I determine if a variable is empty?
For example, in PHP it is empty($variable), what is it in the script?
Thanks
|
|
04-28-2008 10:36 AM |
|
|
Skarbo
New Member
Posts: 7
38 / /
Joined: Apr 2008
|
RE: Determining Empty variables
Some methods:
($variable == null)
or just
(!$variable)
|
|
04-28-2008 10:41 AM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: Determining Empty variables
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
This post was edited on 04-28-2008 at 04:16 PM by matty.
|
|
04-28-2008 01:20 PM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: Determining Empty variables
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...
This post was edited on 04-28-2008 at 04:14 PM by Matti.
|
|
04-28-2008 04:14 PM |
|
|
vikke
Senior Member
Posts: 900 Reputation: 28
31 / /
Joined: May 2006
|
RE: Determining Empty variables
Mattike: == is the equal operator, not ===.
|
|
04-28-2008 04:41 PM |
|
|
matty
Scripting Guru
Posts: 8336 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: Determining Empty variables
quote: Originally posted by vikke
Mattike: == is the equal operator, not ===.
=== Is the bitwise-equal operator.
This post was edited on 04-28-2008 at 06:00 PM by matty.
|
|
04-28-2008 05:55 PM |
|
|
John Anderton
Elite Member
Posts: 3908 Reputation: 80
37 / /
Joined: Nov 2004
Status: Away
|
RE: Determining Empty variables
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
[
KarunAB.com]
[img]http://gamercards.exophase.com/459422.png[
/img]
|
|
04-28-2008 06:00 PM |
|
|
MeEtc
Patchou's look-alike
In the Shadow Gallery once again
Posts: 2200 Reputation: 60
38 / /
Joined: Nov 2004
Status: Away
|
|
04-28-2008 10:10 PM |
|
|
Stigmata
Veteran Member
Posts: 3520 Reputation: 45
21 / /
Joined: Jul 2003
|
RE: Determining Empty variables
I normally use (!$variable)?
or is this bad practice?
|
|
04-28-2008 10:49 PM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: Determining Empty variables
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.
|
|
04-29-2008 04:30 PM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|