Working with Flags in PHP

Now we will learn to work with flags. A flag is a special variable that can only take two values: true and false. With the help of flags, you can solve problems that check for the absence of something: for example, you can check that an array has no element with a specific value. Let's begin.

Let's solve the following problem: given an array with numbers, we need to check if it contains an element with the value 3 or not. If it does - output '+++', if not - output '---'.

First, let's try to output '+++'. To do this, we will iterate over all elements of our array and use an if statement to ask - is the current element equal to the value 3. If it is - output '+++':

php<?php
  $arr = [1, 2, 3, 4, 5];
  
  foreach ($arr as $elem) {
    if ($elem === 3) {
      echo '+++';
    }
  }
?>

But our solution is not very good: if the array has not one value 3, but several, then '+++' will be output several times. Let's modify our array (make two elements with the value 3) and see for ourselves:

php<?php
  $arr = [1, 2, 3, 4, 3, 5];
  
  foreach ($arr as $elem) {
    if ($elem === 3) {
      echo '+++'; // will output several times
    }
  }
?>

Let's fix the problem: terminate the loop using break, if the element is already found:

php<?php
  $arr = [1, 2, 3, 4, 3, 5];
  
  foreach ($arr as $elem) {
    if ($elem === 3) {
      echo '+++';
      break;  // terminate the loop
    }
  }
?>

Now let's try to make it so that, if there are no elements with the value 3 in the array at all, '---' is output. A common misconception is to add else to our if statement - in this case '---' will be output for all elements that are not 3:

php<?php
  $arr = [1, 2, 3, 4, 5];
  
  foreach ($arr as $elem) {
    if ($elem === 3) {
      echo '+++'; // will output on element 3
    } else {
      echo '---'; // will output on elements 1, 2, 4, 5
    }
  }
?>

So, the idea to add else is a bad idea, it doesn't work. To solve problems of this type (such problems are encountered quite often), so-called flags are used.

As mentioned above, a flag is a variable that can take two values: true or false.

So, let's make a variable flag with the following meaning: if it is equal to true, then the array has an element 3, and if false, then there is no such element.

Initially, set the variable flag to the value false - that is, we will assume that the element 3 is not in the array:

javascript$arr = [1, 2, 3, 4, 5];
$flag = false; // assume that element 3 is not in the array

Then we run a loop with an if statement as we did before. If the loop detects that the array has an element 3 - then set the variable flag to the value true and exit the loop with break:

php<?php
  $arr = [1, 2, 3, 4, 5];
  $flag = false; // assume that element 3 is not in the array
  
  foreach ($arr as $elem) {
    if ($elem === 3) {
      $flag = true; // element exists - redefine the variable $flag
      break; // exit the loop
    }
  }
?>

We can only answer the question of whether the array contains 3 or not after the loop. And we already have this answer: after the loop, the variable flag could have remained false or could have changed its value to true, if the loop detected 3 in the array:

php<?php
  $arr = [1, 2, 3, 4, 5];
  $flag = false;
  
  foreach ($arr as $elem) {
    if ($elem === 3) {
      $flag = true;
      break;
    }
  }
  
  // here the variable $flag is either true or false
?>

Now after the loop we can make an if statement that will look at the variable flag and output '+++' or '---' to the screen:

php<?php
  $arr = [1, 2, 3, 4, 5];
  $flag = false;
  
  foreach ($arr as $elem) {
    if ($elem === 3) {
      $flag = true;
      break;
    }
  }
  
  if ($flag === true) {
    echo '+++';
  } else {
    echo '---';
  }
?>
Задание 1

Given an array:

php<?php
  $arr = ['a', 'b', 'c', 'd', 'e'];
?>

Check if this array contains the element 'c'. If it does - output '+++', and if not - output '---'.

Задание 2

Write code that will check whether a number is prime or not. A prime number is divisible only by one and by itself, and is not divisible by other numbers.