Checking the Existence of an Array Element in PHP
Using isset
you can check the existence
of an array element:
<?php
$arr = ['a', 'b', 'c'];
if (isset($arr[5])) {
echo $arr[5];
} else {
echo 'element does not exist';
}
?>
You can also check an element of a non-existent array:
<?php
if (isset($arr[5])) {
echo $arr[5];
} else {
echo 'array or element does not exist';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = 0;
if (isset($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = null;
if (!isset($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = null;
if (isset($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
if (!isset($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = '';
if (isset($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
if (!isset($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$test = false;
if (isset($test)) {
echo '+++';
} else {
echo '---';
}
?>
Without running the code, determine what will be displayed on the screen:
<?php
$arr = [1, 2, 3, 4, 5];
if (isset($arr[])) {
echo '+++';
} else {
echo '---';
}
?>