title: Invalid argument supplied for foreach()
body: <p>It often happens to me to handle data that can be either an array or a null variable and to feed some <code>foreach</code> with these data.</p>
  
  <pre><code>$values = get_values();
  
  foreach ($values as $value){
    ...
  }
  </code></pre>
  
  <p>When you feed a foreach with data that are not an array, you get a warning:</p>
  
  <blockquote>
    <p>Warning: Invalid argument supplied for foreach() in [...]</p>
  </blockquote>
  
  <p>Assuming it's not possible to refactor the <code>get_values()</code> function to always return an array (backward compatibility, not available source code, whatever other reason), I'm wondering which is the cleanest and most efficient way to avoid these warnings:</p>
  
  <ul>
  <li>Casting <code>$values</code> to array</li>
  <li>Initializing <code>$values</code> to array</li>
  <li>Wrapping the <code>foreach</code> with an <code>if</code></li>
  <li>Other (please suggest)</li>
  </ul>