If-else konstruktsiyasida qisqa if
Aytaylik, $test o‘zgaruvchisi true ga tengligini tekshirmoqchimiz. Bunday holda, if sharti quyidagicha yoziladi:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
Dasturlashda bunday tekshiruvlar juda ko‘p uchraydi, shuning uchun qulay qisqartirilgan shakl mavjud:
if ($test == true) o‘rniga if ($test) deb yozish kifoya.
Keling, kodimizni qisqartirilgan shaklda yozamiz:
<?php
$test = true;
if ($test) { // if ($test == true) bilan bir xil
echo '+++';
} else {
echo '---';
}
?>
Endi $test o‘zgaruvchisi true ga teng emasligini tekshiramiz:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
Bu holatda qisqartirilgan sintaksis quyidagicha bo‘ladi:
<?php
$test = true;
if (!$test) { // mantiqiy ! (EMAS) operatoridan foydalanamiz
echo '+++';
} else {
echo '---';
}
?>
false ni tekshirish uchun ham qisqartirilgan yozuv mavjud. Masalan, quyidagi kod berilgan bo‘lsin:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
$test == false o'zi $test != true bilan bir xil:
<?php
$test = true;
if ($test != true) { // if ($test == false) bilan bir xil
echo '+++';
} else {
echo '---';
}
?>
Bunday shartni oldingi misolda qisqartirishni o‘rganganmiz. Keling, qisqartiramiz:
<?php
$test = true;
if (!$test) {
echo '+++';
} else {
echo '---';
}
?>
№1
Qisqartirilgan taqqoslash yordamida quyidagi kodni qayta yozing:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
№2
Qisqartirilgan taqqoslash yordamida quyidagi kodni qayta yozing:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
№3
Qisqartirilgan taqqoslash yordamida quyidagi kodni qayta yozing:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
№4
Qisqartirilgan taqqoslash yordamida quyidagi kodni qayta yozing:
<?php
$test = true;
if ($test != false) {
echo '+++';
} else {
echo '---';
}
?>