PHP-da empty buyrug‘i
Skriptlarda ko‘pincha o‘zgaruvchini bo‘sh yoki yo‘qligini
teksirish zarurati tug‘iladi. PHP-da o‘zgaruvchi 0, '',
'0', false yoki null ga teng bo‘lsa, u bo‘sh hisoblanadi.
Bo‘shlikni tekshirish empty buyrug‘i yordamida amalga oshiriladi:
<?php
$test = '';
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Biroq, ko‘proq uchraydigan vazifa - o‘zgaruvchining bo‘sh emasligini tekshirishdir. Keling, shartimizni teskarisiga aylantiramiz:
<?php
$test = '';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = 0;
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = -1;
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = '';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = -1;
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = '0';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = -1;
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = null;
if (empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = false;
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = true;
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = 'false';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>
Kodni ishlamasdan, ekranda chiqadigan narsani aniqlang:
<?php
$test = 'null';
if (!empty($test)) {
echo '+++';
} else {
echo '---';
}
?>