Kısaltılmış if-else Yapısı
Diyelim ki, örneğin, $test değişkeninin
değerinin true olup olmadığını öğrenmek
istiyoruz.
Bu durumda if yapısını
şu şekilde yazabiliriz:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
Programlama sırasında bu tür kontrollere çok sık
ihtiyaç duyulduğu için, bunlar için daha zarif
bir kısaltılmış form vardır: if ($test
== true) yerine sadece if
($test) yazılabilir.
Kodumuzu kısaltılmış forma yeniden yazalım:
<?php
$test = true;
if ($test) { // if ($test == true) ile eşdeğer
echo '+++';
} else {
echo '---';
}
?>
Şimdi diyelim ki $test değişkeninin
true'ya eşit olmadığını kontrol ediyoruz:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
Bu durumda kısaltılmış sözdizimi şu şekilde görünecektir:
<?php
$test = true;
if (!$test) { // mantıksal DEĞİL kullanıyoruz
echo '+++';
} else {
echo '---';
}
?>
false kontrolü için de benzer bir kısaltma
mevcuttur. Şu kodu ele alalım:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
$test == false koşulu aslında
$test != true ile aynı şeydir:
<?php
$test = true;
if ($test != true) { // if ($test == false) ile eşdeğer
echo '+++';
} else {
echo '---';
}
?>
Ve böyle bir koşulu zaten bir önceki örnekte kısaltmayı öğrenmiştik. Kısaltalım:
<?php
$test = true;
if (!$test) {
echo '+++';
} else {
echo '---';
}
?>
Aşağıdaki kodu kısaltılmış karşılaştırma kullanarak yeniden yazın:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
Aşağıdaki kodu kısaltılmış karşılaştırma kullanarak yeniden yazın:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
Aşağıdaki kodu kısaltılmış karşılaştırma kullanarak yeniden yazın:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
Aşağıdaki kodu kısaltılmış karşılaştırma kullanarak yeniden yazın:
<?php
$test = true;
if ($test != false) {
echo '+++';
} else {
echo '---';
}
?>