isset
CHECKS if one or more
VARIABLES are SET AND are NOT NULL.
This function returns a BOOLEAN 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 TO TEST CONSTANTS
The test of more than one variable:
IT WILL ONLY HAVE EFFECT IF ALL ARE DEFINED.
Evaluation goes from left to right and stops as soon as an unset variable is encountered.
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - -
To determine if a VARIABLE
IS SET and IS NOT NULL
- - - - - - - - - - - - - - - - - - - - - - - - */
bool isset ( mix $var1, [mix var2, ..., mix $varN ] );
where,
$var1 = FIRST VARIABLE to test
$var2 = SECOND VARIABLE to test
. . . . . . . . . . . . . . .
$varN = LAST VARIABLE to test
?>
$var1
The first variable to test.
$var2 ... $varN
The rest of other variables to test.
EXERCISE
<?php
function ISsett ($var)
{
if(isset($var))
{
var_dump($var);
echo '<br>THIS VARIABLE IS SET<br><br>';
}
else
{
var_dump($var);
echo '<br>THIS VARIABLE IS NOT 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 nwteste
{
var $var1a = 'Isto é um objeto teste';
var $var1b = 'This is a test object';
}
$var01h = new nwteste;
// echo $var01h->var1a;
// echo $var01h->var1b;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The @ character before the
command for the function,
is intended to
avoid displaying errors.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ISsett($var01a);
ISsett($var01b);
ISsett($var01f);
ISsett($var01g);
ISsett($var01h);
?>
RESULT
PHP 7.4.XX
- - - - - - - - - - - - - - - - - - - - - -
NULL
THIS VARIABLE IS NOT SET
- - - - - - - - - - - - - - - - - - - - - -
int(100)
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
float(0.63661977236758)
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
resource(9) of type (gd)
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
object(nwteste)#1 (2) { ["var1"]=> string(23) "Isto é um objeto teste" ["var2"]=> string(21) "This is a test object" }
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
PHP 8.0.XX
- - - - - - - - - - - - - - - - - - - - - -
NULL
THIS VARIABLE IS NOT SET
- - - - - - - - - - - - - - - - - - - - - -
int(100)
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
float(0.63661977236758)
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
object(GdImage)#1 (0) { }
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
object(nwteste)#1 (2) { ["var1"]=> string(23) "Isto é um objeto teste" ["var2"]=> string(21) "This is a test object" }
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
!isset = not isset
Checks if one or more variables have not yet been defined or if they have been made undefined by the use of the unset function.
This function is a negative for the isset function.
This version of the isset function returns a BOOLEAN 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 to test constants
The test of more than one variable:
IT WILL ONLY HAVE EFFECT IF ALL ARE DEFINED
Evaluation goes from left to right and stops as soon as an unset variable is encountered.
<?php
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Determine if a VARIABLE
IS NOT SET
It can be used after an unset or
if the variable has not been set, yet
The unset function
will be studied in the sequence
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
bool !isset ( mix $var1, [mix var2, ..., mix $varN ] );
where,
$var1 = FIRST VARIABLE to test
$var2 = SECOND VARIABLE to test
. . . . . . . . . . . . . . .
$varN = LAST VARIABLE to test
?>
$var1
The first variable to test.
$var2 ... $varN
The rest of other variables to test.
EXERCISE
<?php
function notISsett ($var)
{
if(!isset($var))
{
var_dump($var);
echo '<br>THIS VARIABLE IS NOT SET<br><br>';
}
else
{
var_dump($var);
echo '<br>THIS VARIABLE IS 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 nwteste
{
var $var2a = 'Isto é um objeto teste';
var $var2b = 'This is a test object';
}
$var02h = new nwteste;
// echo $var02h->var2a;
// echo $var02h->var2b;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The @ character before the
command for the function,
is intended to
avoid displaying errors
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
@notISsett($var02a);
@notISsett($var02b);
@notISsett($var02f);
@notISsett($var02g);
@notISsett($var02h);
?>
RESULT
- - - - - - - - - - - - - - - - - - - - - -
NULL
THIS VARIABLE IS NOT SET
- - - - - - - - - - - - - - - - - - - - - -
int(100)
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
float(0.63661977236758)
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - -
PHP 7.4.XX
resource(9) of type (gd)
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
PHP 8.0.XX
object(GdImage)#1 (0) { }
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - -
object(nwteste)#1 (2) { ["var1"]=> string(23) "Isto é um objeto teste" ["var2"]=> string(21) "This is a test object" }
THIS VARIABLE IS SET
- - - - - - - - - - - - - - - - - - - - - -
EXERCISE
<?php
$var03a = NULL;
$var03b = 100;
$var03c = array(2, 'alô', 'hello');
$var03d = true;
$var03e = 'Quandoque bonus dormitat Homerus!';
$var03f = M_2_PI;
$var03g = imagecreate(1,2);
class nwteste
{
var $var3a = 'Isto é um objeto teste';
var $var3b = 'This is a test object';
}
$var03h = new nwteste;
// echo $var03h->var3a;
// echo $var03h->var3b;
$bo3 = isset($var03a, $var03b,
$var03c, $var03d, $var03e,
$var03f, $var03g, $var03h);
if ($bo3 == false)
{
echo 'At least one of the variables
tested has a NULL value or is not set or
it has a CONSTANT value.<br><br>';
}
else
{
echo 'All tested variables are defined.<br>
None has NULL value.<br><br>';
}
?>
RESULT
At least one of the variables tested has a NULL value or is not set or it has a CONSTANT value.
This exercise cannot be run concurrently with the previous exercise.
If this occurs, a Fatal error: ... will be issued.
So check it out.