unsetDESTROYS VARIABLE settings.
This function returns a VOID value when it tests the following variable types:
STRING, NUMERIC, BOOLEAN, ARRAY, NULL, RESOURCE or OBJECT.
One way to visualize the test result is to use var_dump, however, it is better to use if as a conditional evaluation framework.
CAN NOT BE USED OVER CONSTANTS
Its BEHAVIOR may be AFFECTED,
in the case of VARIABLES TREATED BY FUNCTIONS.
Evaluation goes from left to right and stops as soon as an unset variable is encountered.
All variables destroyed by unset are set to NULL.
Because it is a simple language construction, it can not be considered a function.
Therefore, it can not be called using variable functions, which is why we use isset and !isset and not unset or !unset to verify its effectiveness.
It is possible to unset even object properties visible in current context.
Since PHP 5.0.0 it is not possible to unset variables inside an OBJECT.
<?php
void unset ( mix $var1, [mix var2, ..., mix $varN ] );
where,
$var1 = FIRST VARIABLE to destroy
$var2 = SECOND VARIABLE to destroy
. . . . . . . . . . . . . . .
$varN = LAST VARIABLE to destroy
?>
$var1
The first variable to destroy.
$var2 ... $varN
The rest of other variables to destroy.
EXERCISE
<?php
function UNsett01 ($var)
{
if(!isset($var))
{
var_dump($var);
echo '<br>THIS VARIABLE IS UNSET<br><br>';
}
else
{
var_dump($var);
echo '<br>THIS VARIABLE IS YET SET<br><br>';
}
}
$var01a = NULL;
$var01b = 100;
$var01c = array(2, 'alô', 'hello');
$var01d = true;
$var01e = 'Quandoque bonus dormitat Homerus!';
$var01f = M_2_PI;
$var01g = imagecreate(1,2);
class nwteste01
{
var $var1a = 'Isto é um objeto teste';
var $var1b = 'This is a test object';
}
$var01h = new nwteste01;
// echo $var01h->var1a;
// echo $var01h->var1b;
unset($var01b);
@UNsett01($var01b);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The @ character before the
the function name,
is intended to
avoid displaying errors
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
@UNsett01($var01f);
@UNsett01($var01g);
unset($var01h);
@UNsett01($var01h);
?>
EXERCISE
<?php
function UNsett02 ($var)
{
if(!isset($var))
{
var_dump($var);
echo '<br>THIS VARIABLE IS UNSET<br><br>';
}
else
{
var_dump($var);
echo '<br>THIS VARIABLE IS YET SET<br><br>';
}
}
$var02a = NULL;
$var02b = 100;
$var02c = array(2, 'alô', 'hello');
$var02d = true;
$var02e = 'Quandoque bonus dormitat Homerus!';
$var02f = M_2_PI;
$var02g = imagecreate(1,2);
class nwteste02
{
var $var02h = 'Isto é um objeto teste';
var $var02i = 'This is a test object';
}
$var02j = new nwteste02;
// echo $var02j->var2h . '<br>';
// echo $var02j->var2i . '<br><br>';
unset($var02a, $var02b, $var02c, $var02f, $var02j );
@UNsett02($var02a);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The @ character before the
command for the function,
is intended to
avoid displaying errors.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
@UNsett02($var02b);
@UNsett02($var02c);
@UNsett02($var02d);
@UNsett02($var02e);
@UNsett02($var02f);
@UNsett02($var02g);
@UNsett02($var02j);
@UNsett02($var02h);
?>