Colin Brogan
Colin Brogan

Reputation: 748

Is undefined a data-type in php?

Is undefined a data-type in php? Also, how does one check for it (on a variable, is or is not undefined)?

Upvotes: 2

Views: 10422

Answers (6)

Ivan Dossev
Ivan Dossev

Reputation: 565

(might be a bit over the top)

Here are two fairly "simple" ways to mimic your own undefined type. With some overhead for a function call it can be available to your code without using the keyword global.

<?php
class undef1{function __toString(){return 'undefined';}}
function undef1(){
    static $C;
    if($C===null){$C = new undef1();}
    return $C;
}

echo 'undef1 in string context : '; var_dump( undef1().'');
echo 'undef1 in boolean context: '; var_dump( !!undef1() );
echo 'undef1 compared to itself: '; var_dump( (undef1() === undef1()) );


class undef2 extends SimpleXMLElement{function __toString(){return 'undefined';}}
function undef2(){
    static $C;
    if($C===null){$C = new undef2('<C/>');}
    return $C;
}

echo 'undef2 in string context : '; var_dump( undef2().'');
echo 'undef2 in boolean context: '; var_dump( !!undef2() );
echo 'undef2 compared to itself: '; var_dump( (undef2() === undef2()) );
?>

the output (PHP 5.3.23):

undef1 in string context : string(9) "undefined" // :)
undef1 in boolean context: bool(true)            // :(
undef1 compared to itself: bool(true)            // :)

undef2 in string context : string(0) ""    // :( ?
undef2 in boolean context: bool(false)     // :)
undef2 compared to itself: bool(true)      // :)

In either case it seems ticky to get the thing to feel like JavaScript.

To get the object to evaluate to boolean false the second solution uses a hack with the way PHP handles SimpleXMLElement objects. However that same hack can backfire if some code alters the XML value of your undef2 object.

Sadly the magic function __toString() becomes ironically useless when extending the SimpleXMLElement class. Maybe there is another function to overload that will return a custom string but I cannot find it. Though maybe an empty string is actually more practical.

There are PECL operator overloads that could maybe get the undef1 class to evaluate to boolean false, but the extension is beta and might not be available to your PHP code.

Misc notes:

It seems that an undefined type would make a lot of sense for custom classes that implement ArrayAccess, where it is be possible to:

  • ignore creation of elements if the $value === undefined
  • return undefined if a key or index is not defined

In the example above the class is not restricted to a singleton. You can make it behave that way if you wish: Creating the Singleton design pattern in PHP5

Upvotes: 0

Jon
Jon

Reputation: 437734

There is no "undefined" data type in PHP. You can check for a variable being set with isset, but this cannot distinguish between a variable not being set at all and it having a null value:

var_dump(isset($noSuchVariable)); // false

$nullVariable = null;
var_dump(isset($nullVariable)); // also false

However, there is a trick you can use with compact that allows you to determine if a variable has been defined, even if its value is null:

var_dump(!!compact('noSuchVariable')); // false
var_dump(!!compact('nullVariable')); // true

Live example.

Both isset and the compact trick also work for multiple variables at once (use a comma-separated list).

You can easily distinguish between a null value and total absence when dealing with array keys:

$array = array('nullKey' => null);

var_dump(isset($array['nullKey'])); // false
var_dump(array_key_exists($array, 'nullKey')); // true

Live example.

When dealing with object properties there is also property_exists, which is the equivalent of array_key_exists for objects.

Upvotes: 6

Daryl Gill
Daryl Gill

Reputation: 5524

There isn't undefined, but there is null different yes. But undefined is not a valid Data type, all variables need to be defined. By:

$Foo = "Test"; 
if (isset($Foo))
{
 echo "Variable Is Defined";
}
else
{
 echo "Variable Is not Defined";
}



if (isset($UndefinedVar))
{
 echo "Variable Is Defined";
}
else
{
echo "Variable Is Not Defined";
}

Your Outputs for each statement would be:

1) Variable is Defined

2) Variable Is Not Defined


If undefined variables was a valid datatype within PHP or any programming languages, it would take the ability to work with variables, because essentially.. They would already be in use

Upvotes: 1

mario
mario

Reputation: 145512

NULL is the implicit value for undefined variables. isset will not work, as it ignores variables initialized to =NULL.

To probe if a variable is really present, you have to use a workaround:

if (in_array("varname", array_keys(get_defined_vars()))) {

Upvotes: 3

Devon Bernard
Devon Bernard

Reputation: 2300

To check if a variable is defined or not you can try this:

<?php
if(isset($myvar)){
echo 'your variable is set as' . $myvar;
}else{
echo 'your variable is not set';
}
?>

Also as far as I know 'undefined' is not a data-type in PHP.

Upvotes: 2

nickb
nickb

Reputation: 59709

No, undefined is not a data type in PHP. You check if a variable is set (i.e. previously defined and not null) in PHP with isset():

if( isset( $foo)) { 
    echo "Foo = $foo\n";
} else {
    echo "Foo is not set!\n";
}

From the docs, isset() will:

Determine if a variable is set and is not NULL.

Upvotes: 3

Related Questions