Undefined values: Difference between revisions

From Rosetta Code
Content added Content deleted
(added php)
Line 24: Line 24:
undef $var;
undef $var;


# Check to see whether it is defined after we've explicitely
# Check to see whether it is defined after we've explicitly
# given it an undefined value.
# given it an undefined value.
print "var is undefined at third check\n" unless defined $var;
print "var is undefined at third check\n" unless defined $var;
Line 36: Line 36:


# Because most of the output is conditional, this serves as
# Because most of the output is conditional, this serves as
# a clear indicator that the progrma has run to completion.
# a clear indicator that the program has run to completion.
print "Done\n";</lang>
print "Done\n";</lang>


Line 45: Line 45:
Done
Done
</pre>
</pre>

=={{header|PHP}}==

<lang php><?php
// Check to see whether it is defined
if (!isset($var))
echo "var is undefined at first check\n";

// Give it a value
$var = "Chocolate";

// Check to see whether it is defined after we gave it the
// value "Chocolate"
if (!isset($var))
echo "var is undefined at second check\n";

// Give the variable an undefined value.
unset($var);

// Check to see whether it is defined after we've explicitly
// given it an undefined value.
if (!isset($var))
echo "var is undefined at third check\n";

// Give the variable a value of 42
$var = 42;

// Check to see whether the it is defined after we've given it
// the value 42.
if (!isset($var))
echo "var is undefined at fourth check\n";

// Because most of the output is conditional, this serves as
// a clear indicator that the program has run to completion.
echo "Done\n";
?></lang>

Results in:

<pre>var is undefined at first check
var is undefined at third check
Done
</pre>

{{omit from|Java|everything is null or defined}}
{{omit from|Java|everything is null or defined}}

Revision as of 06:32, 28 November 2009

Task
Undefined values
You are encouraged to solve this task according to the task description, using any language you may know.

For languages which have an explicit notion of an undefined value, (as opposed to a NULL value), identify and exercise those language's mechanisms for identifying and manipulating a variable's value's status as being undefined

Perl

<lang perl>#!/usr/bin/perl -w use strict;

  1. Declare the variable

our $var;

  1. Check to see whether it is defined

print "var is undefined at first check\n" unless defined $var;

  1. Give it a value

$var = "Chocolate";

  1. Check to see whether it is defined after we gave it the
  2. value "Chocolate"

print "var is undefined at second check\n" unless defined $var;

  1. Give the variable an undefined value.

$var = undef;

  1. or, equivalently:

undef $var;

  1. Check to see whether it is defined after we've explicitly
  2. given it an undefined value.

print "var is undefined at third check\n" unless defined $var;

  1. Give the variable a value of 42

$var = 42;

  1. Check to see whether the it is defined after we've given it
  2. the value 42.

print "var is undefined at fourth check\n" unless defined $var;

  1. Because most of the output is conditional, this serves as
  2. a clear indicator that the program has run to completion.

print "Done\n";</lang>

Results in:

var is undefined at first check
var is undefined at third check
Done

PHP

<lang php><?php // Check to see whether it is defined if (!isset($var))

   echo "var is undefined at first check\n";

// Give it a value $var = "Chocolate";

// Check to see whether it is defined after we gave it the // value "Chocolate" if (!isset($var))

   echo "var is undefined at second check\n";

// Give the variable an undefined value. unset($var);

// Check to see whether it is defined after we've explicitly // given it an undefined value. if (!isset($var))

   echo "var is undefined at third check\n";

// Give the variable a value of 42 $var = 42;

// Check to see whether the it is defined after we've given it // the value 42. if (!isset($var))

   echo "var is undefined at fourth check\n";

// Because most of the output is conditional, this serves as // a clear indicator that the program has run to completion. echo "Done\n"; ?></lang>

Results in:

var is undefined at first check
var is undefined at third check
Done