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<?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<?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<?php
  $test = true;
  
  if ($test != true) {
    echo '+++';
  } else {
    echo '---';
  }
?>

Bu durumda kısaltılmış sözdizimi şu şekilde görünecektir:

php<?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<?php
  $test = true;
  
  if ($test == false) {
    echo '+++';
  } else {
    echo '---';
  }
?>

$test == false koşulu aslında $test != true ile aynı şeydir:

php<?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<?php
  $test = true;
  
  if (!$test) {
    echo '+++';
  } else {
    echo '---';
  }
?>
Задание 1

Aşağıdaki kodu kısaltılmış karşılaştırma kullanarak yeniden yazın:

php<?php
  $test = true;
  
  if ($test == true) {
    echo '+++';
  } else {
    echo '---';
  }
?>
Задание 2

Aşağıdaki kodu kısaltılmış karşılaştırma kullanarak yeniden yazın:

php<?php
  $test = true;
  
  if ($test == false) {
    echo '+++';
  } else {
    echo '---';
  }
?>
Задание 3

Aşağıdaki kodu kısaltılmış karşılaştırma kullanarak yeniden yazın:

php<?php
  $test = true;
  
  if ($test != true) {
    echo '+++';
  } else {
    echo '---';
  }
?>
Задание 4

Aşağıdaki kodu kısaltılmış karşılaştırma kullanarak yeniden yazın:

php<?php
  $test = true;
  
  if ($test != false) {
    echo '+++';
  } else {
    echo '---';
  }
?>