How to insert foreach after echo? <p>this might be a stupid question so please accept my apologies.</p> <p>I have written a code snippet for a wordpress website that lists restaurants. You enter the instagram id of the venues (if they have one) and it shows the latest instagram photos they've uploaded. If the instagram id value is left empty, i want it to echo nothing. I've written the code and it works fine for places with instagram id's, but as I'm unable the last piece of code (that starts with foreach) inside the echo section, it tries to show it even when the id is empty hence i get a "Invalid argument supplied for foreach() in" error. Can you help me out so that I can include that code inside the echo part, so that it won't call that code when id is empty? Thanks very much in advance.</p> <pre><code> &lt;?php if ( $instagramid ) { echo " &lt;br&gt;&lt;br&gt; &lt;span style=\"float:left; -webkit-border-radius: 3px; border-radius: 3px; font-size: 14px; float: left; line-height: 32px; margin-right: 5px; margin-top:12px; padding: 0px 3px 0px 5px; \"&gt; &lt;a target=\"_blank\" href=\"http://instagram.com/".$username."\"&gt;&lt;img width=\"80\" border=\"1px\" src=\"".$profilepic."\"&gt;&lt;/a&gt;&lt;/span&gt; &lt;br&gt; &lt;div style=\"margin-left: 50px;\"&gt; &lt;div style=\"position: relative; float: left; left: 0.00%; width: 75.00%; background-color: #f4f4f4\"&gt; &lt;span style=\" font-weight: bold; font-style: normal; font-size: 12px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \"&gt;&lt;a style=\"color: #3f729b; font-weight: bold; \" target=\"_blank\" href=\"http://instagram.com/".$username."\"&gt;".$username."&lt;/a&gt; @ instagram&lt;/span&gt; &lt;/div&gt; &lt;div style=\"position: relative; float: left; left: 0.00%; width: 82.00%;\"&gt; &lt;div style=\"position: relative; float: left; left: 0.50%; width: 33.00%;\"&gt; &lt;span style=\" font-weight: lighter; font-style: normal; font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \"&gt;Photos&lt;br&gt; &lt;span style=\" font-weight: bold; \"&gt;&lt;span style=\" letter-spacing: 0px;\"&gt;".$sayi."&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/div&gt; &lt;div style=\"position: relative; float: left; left: 1.50%; width: 33.00%;\"&gt; &lt;span style=\" font-weight: lighter; font-style: normal; font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \"&gt;Followers&lt;br&gt; &lt;span style=\" font-weight: bold;\"&gt;&lt;span style=\" letter-spacing: 0px;\"&gt;".$takipci."&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt; &lt;/div&gt; &lt;div style=\"position: relative; float: right; right: 0.50%; width: 31.00%;\"&gt; &lt;span style=\" font-weight: lighter; font-style: normal; font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \"&gt;Following&lt;br&gt;&lt;span style=\" font-weight: bold;\"&gt;&lt;span style=\" letter-spacing: 0px;\"&gt;".$takipediyor."&lt;/span&gt;&lt;/span&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;br&gt;&lt;br&gt; "; } else { echo ""; } ?&gt; &lt;?php foreach ($result-&gt;data as $fotos): ?&gt; &lt;!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) --&gt; &lt;span style="font-size: 12px; float:left; margin:5px 0 2px 10px;"&gt; &lt;a target="_blank" class="group" rel="group1" href="&lt;?= $fotos-&gt;link ?&gt;"&gt; &lt;img width="90" src="&lt;?= $fotos-&gt;images-&gt;thumbnail-&gt;url ?&gt;"&gt;&lt;/a&gt; &lt;span style="font-weight:bold; float:left;"&gt;&lt;/span&gt; &lt;span style="float:right;"&gt;&lt;span style="color:red; font-weight: bolder;"&gt;❤ &lt;/span&gt;&lt;?= $fotos-&gt;likes-&gt;count ?&gt;&lt;/span&gt; &lt;/span&gt;&lt;?php endforeach //Biter ?&gt; </code></pre>