The empty Command in PHP
Often in scripts, there is a need to check
a variable for emptiness. In PHP, a variable
will be empty if it is equal to 0
, ''
,
'0'
, false
or null
.
The check for emptiness is performed using
the empty
command:
<?php
$test = '';
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
More often, however, the inverse task arises - checking that a variable is not empty. Let's invert our condition:
<?php
$test = '';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = 0;
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = -1;
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = '';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = -1;
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = '0';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = -1;
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = null;
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = false;
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = true;
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = 'false';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = 'null';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>