If-else konstruksiyasida qisqartirilgan if
Faraz qilaylik, masalan, $test o'zgaruvchisi
true qiymatiga teng yoki yo'qligini bilmoqchimiz.
Bunday holda if konstruksiyasini
quyidagicha yozishimiz mumkin:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
Dasturlashda bunday tekshiruvlar juda ko'p
kerak bo'ladi, shuning uchun ular uchun yanada
chiroyli qisqartirilgan shakl mavjud: if ($test
== true) o'rniga shunchaki if
($test) deb yozish mumkin.
Keling, kodimizni qisqartirilgan shaklda qayta yozamiz:
<?php
$test = true;
if ($test) { // if ($test == true) ga ekvivalent
echo '+++';
} else {
echo '---';
}
?>
Endi faraz qilaylik, $test o'zgaruvchisi
true ga teng *emasligini* tekshirmoqchimiz:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
Bunday holda qisqartirilgan sintaksis quyidagicha bo'ladi:
<?php
$test = true;
if (!$test) { // mantiqiy NOT (YO'Q) dan foydalanamiz
echo '+++';
} else {
echo '---';
}
?>
false ni tekshirish uchun ham shunga o'xshash
qisqartirma mavjud. Quyidagi kod berilgan deb faraz qilaylik:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
$test == false sharti aslida
$test != true bilan bir xil:
<?php
$test = true;
if ($test != true) { // if ($test == false) ga ekvivalent
echo '+++';
} else {
echo '---';
}
?>
Xo'sh, bunday shartni biz oldingi misolda qisqartirishni o'rgangan edik. Qisqartiramiz:
<?php
$test = true;
if (!$test) {
echo '+++';
} else {
echo '---';
}
?>
Quyidagi kodni qisqartirilgan taqqoslashdan foydalangan holda qayta yozing:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
Quyidagi kodni qisqartirilgan taqqoslashdan foydalangan holda qayta yozing:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
Quyidagi kodni qisqartirilgan taqqoslashdan foydalangan holda qayta yozing:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
Quyidagi kodni qisqartirilgan taqqoslashdan foydalangan holda qayta yozing:
<?php
$test = true;
if ($test != false) {
echo '+++';
} else {
echo '---';
}
?>