Optimization of Repeated Operations in PHP

Often, beginner programmers thoughtlessly call the same function multiple times, wasting resources.

Let's look at an example. Consider the following code:

php<?php
  $arr = [1, 2, 3, 4, 5];
  
  if (count($arr) >= 1 and count($arr) <= 3) {
    
  }
?>

What's wrong with this code? The fact is that we do the same thing twice - calculate the length of the array using count($arr). This operation takes time and it is advisable to perform it once, assign the result to a variable, and then use this variable where needed, like this:

php<?php
  $arr = [1, 2, 3, 4, 5];
  $len = count($arr);
  
  if ($len >= 1 and $len <= 3) {
    
  }
?>
Задание 1

Optimize the code below:

php<?php
  if (date('Y') >= 2018 and date('Y') <= 2020) {
              echo 'year ' . date('Y') . ' is suitable';
  } else {
      echo 'year ' . date('Y') . ' is not suitable';
  }
?>
Задание 2

Optimize the code below:

php<?php
  $password = 'abcde';
  
  if (strlen($password) >= 2 and strlen($password) <= 10) {
    echo 'password length is acceptable';
  } else {
      echo 'invalid password length';
  }
?>