If prescurtat în construcția if-else

Să presupunem, de exemplu, că vrem să aflăm dacă variabila $test este egală cu valoarea true. În acest caz, construcția if poate fi scrisă astfel:

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

În programare, astfel de verificări sunt necesare foarte des, deci pentru ele există o formă prescurtată mai elegantă: în loc de if ($test == true) se poate scrie simplu if ($test).

Să rescriem codul nostru în forma prescurtată:

php<?php
  $test = true;
  
  if ($test) { // echivalent cu if ($test == true)
    echo '+++';
  } else {
    echo '---';
  }
?>

Să presupunem acum că verificăm dacă variabila $test nu este egală cu true:

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

În acest caz, sintaxa prescurtată va arăta astfel:

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

O abreviere similară există și pentru verificarea la false. Să presupunem că avem următorul cod:

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

Condiția $test == false de fapt este la fel ca $test != true:

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

Ei bine, iar o astfel de condiție am învățat deja să o prescurtăm în exemplul anterior. Să prescurtăm:

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

Rescrieți următorul cod utilizând compararea prescurtată:

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

Rescrieți următorul cod utilizând compararea prescurtată:

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

Rescrieți următorul cod utilizând compararea prescurtată:

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

Rescrieți următorul cod utilizând compararea prescurtată:

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