Sutrumpintas if if-else konstrukcijoje
Tarkime, pavyzdžiui, mes norime sužinoti, ar
kintamasis $test yra lygus reikšmei true.
Šiuo atveju if konstrukciją galima
užrašyti taip:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
Programuojant tokie patikrinimai reikalingi
labai dažnai, todėl jiems yra sukurtas elegantiškesnis
sutrumpintas variantas: vietoj if ($test
== true) galima parašyti tiesiog if
($test).
Perrašykime mūsų kodą sutrumpinta forma:
<?php
$test = true;
if ($test) { // ekvivalentu if ($test == true)
echo '+++';
} else {
echo '---';
}
?>
Tarkime dabar mes tikriname, ar kintamasis
$test nėra lygus true:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
Šiuo atveju sutrumpintas sintaksė atrodys taip:
<?php
$test = true;
if (!$test) { // naudojame loginį NE
echo '+++';
} else {
echo '---';
}
?>
Panašus sutrumpinimas egzistuoja ir patikrinimui
į false. Tarkime, duotas toks kodas:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
Sąlyga $test == false iš tikrųjų
yra tas pats, kas $test != true:
<?php
$test = true;
if ($test != true) { // ekvivalentu if ($test == false)
echo '+++';
} else {
echo '---';
}
?>
O tokią sąlygą mes jau išmokome sutrumpinti ankstesniame pavyzdyje. Sutrumpinkime:
<?php
$test = true;
if (!$test) {
echo '+++';
} else {
echo '---';
}
?>
Perrašykite šį kodą naudodami sutrumpintą palyginimą:
<?php
$test = true;
if ($test == true) {
echo '+++';
} else {
echo '---';
}
?>
Perrašykite šį kodą naudodami sutrumpintą palyginimą:
<?php
$test = true;
if ($test == false) {
echo '+++';
} else {
echo '---';
}
?>
Perrašykite šį kodą naudodami sutrumpintą palyginimą:
<?php
$test = true;
if ($test != true) {
echo '+++';
} else {
echo '---';
}
?>
Perrašykite šį kodą naudodami sutrumpintą palyginimą:
<?php
$test = true;
if ($test != false) {
echo '+++';
} else {
echo '---';
}
?>