title: How can i make a piece of code run if a foreach loop has not output any values?
body: <p>I have an array which I have put into a foreach loop, where each value of the array will be outputted to the user. If the user has entered a search query, the value will be checked againast a regex and only be returned if it matches, otherwise the value is just outputted.</p>
  
  <p>The problem I'm having is I havent been able to figure out how to make a conditional "no results found" output if neither the unconditional or the regex conditional outputs output anything. Code below.</p>
  
  <pre><code>foreach ($result as $value)
  {
      // check to see if query term is set and if so run regex comparison
      if (isset($pattern))
      {
          if (preg_match("/^$pattern/i", $value)) 
          {
              echo $value;
              echo "&lt;br /&gt;";
          }   
      }
  
      // if query is not set, simply output the value
      else
      {
          echo $value;
          echo "&lt;br /&gt;";
      }
  
      // and if there has been no output for either the regex conditional, or other output,                   
      // I want output "no results". How?
  }
  </code></pre>