if-else quruluşunda qısaldılmış if
Tutaq ki, məsələn, biz bilmək istəyirik ki,
$test dəyişəni true qiymətinə bərabərdirmi.
Bu halda if quruluşunu
belə yazmaq olar:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
Proqramlaşdırma zamanı belə yoxlamalar çox
tez-tez tələb olunur, ona görə də onlar üçün daha
zərif qısaldılmış forma mövcuddur: if ($test
== true) əvəzinə sadəcə if
($test) yazmaq olar.
Gəlin kodumuzu qısaldılmış formada yenidən yazaq:
<?php
$test = true;
if ($test) { // ekvivalent if ($test == true)
echo '+++';
} else {
echo '---';
}
?>
Tutaq ki, indi biz $test dəyişəninin
true-yə bərabər OLMAMASINI yoxlayırıq:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
Bu halda qısaldılmış sintaksis belə görünəcək:
<?php
$test = true;
if (!$test) { // məntiqi DEYİL istifadə edirik
echo '+++';
} else {
echo '---';
}
?>
false üçün yoxlamada da oxşar qısaltma
mövcuddur. Tutaq ki, belə bir kod verilmişdir:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
$test == false şərti əslində
$test != true ilə eyni şeydir:
<?php
$test = true;
if ($test != true) { // ekvivalent if ($test == false)
echo '+++';
} else {
echo '---';
}
?>
Yaxşı, belə bir şərti artıq əvvəlki misalda qısaltmağı öyrənmişik. Gəlin qisaldaq:
<?php
$test = true;
if (!$test) {
echo '+++';
} else {
echo '---';
}
?>
Aşağıdakı kodu qısaldılmış müqayisədən istifadə etməklə yenidən yazın:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
Aşağıdakı kodu qısaldılmış müqayisədən istifadə etməklə yenidən yazın:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
Aşağıdakı kodu qısaldılmış müqayisədən istifadə etməklə yenidən yazın:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
Aşağıdakı kodu qısaldılmış müqayisədən istifadə etməklə yenidən yazın:
<?php
$test = true;
if ($test != false) {
echo '+++';
} else {
echo '---';
}
?>