title: Why is the else statement not working inside a foreach loop in php?
body: <p>I'm building a script that helps a user reset their password if they forgot it. This specific script firstly checks if the email the user wants the token sent to exists. If it does, the token is inserted into a tokens table.</p>
  <p>I am still developing and testing, so I haven't created the mail function yet.</p>
  <p>I have an if statement that checks if the email exists, then creates the token. If it doesn't exists, it shows the page to enter an email address again. The if statement is working perfect, but the else is not. I'm pasting the entire file here, though it is only the part with the foreach statement that concerns our problem.</p>
  <p>The else statement shows absolutely nothing.</p>
  <pre><code>&lt;?php
      //generate tokens to verify users who forgot their passwords. Send these tokens to the user's email
      
      require $_SERVER['DOCUMENT_ROOT'] . &quot;/phonebook/config.php&quot;;
      
      //get user's email
      $mail = htmlspecialchars($_POST['email']);
      //generate token
      $token = $token = openssl_random_pseudo_bytes(20);
      //Convert the binary data into something more readable
      $token = bin2hex($token);
      
      //check if the email entered exists
      $check = $conn-&gt;prepare(&quot;SELECT email FROM users WHERE email = :email&quot;);
      $check-&gt;bindParam(&quot;:email&quot;, $mail);
      $check-&gt;execute();
      
      foreach ($check as $confirm) {
          if ($confirm['email'] == $mail) {
              //send tokens to the database
              $sql = $conn-&gt;prepare(&quot;INSERT INTO tokens (email, token)
              VALUES(:email, :token)&quot;);
              
              $sql-&gt;bindParam(&quot;:email&quot;, $mail);
              $sql-&gt;bindParam(&quot;:token&quot;, $token);
              $sql-&gt;execute();
      
              echo &quot;&lt;h2&gt;Enter security code&lt;/h2&gt;
              A security code has been sent to the email addres $mail. Enter that code in the box below:
              &lt;form action = 'http://localhost/phonebook/controls/forgotpassword.php' method = 'post'&gt;
              &lt;input type = 'hidden' name = 'email' value = '$mail'&gt;
              &lt;input type = 'text' name = 'token'&gt; &lt;br&gt;
              &lt;input type = 'submit' value = 'Reset password'&gt;
              &lt;/form&gt;
              Did not receive code? Go &lt;a href = 'http://localhost/pages/forgotpassword.php'here&lt;/a&gt;&quot;;
          } else {
              echo &quot;That email does not exist. Please try again.&quot;;
              include_once &quot;$paste/phonebook/pages/forgotpassword.php&quot;;
      }
  }
  </code></pre>