snipt

Ctrl+h for KB shortcuts
Notice: Snipt is moving away from free accounts on May 1st, 2014. Read more about the transition here.
#43596

Text only

Break a loop after a certain number

1
2
3
4
5
6
7
8
$max_loop=5; //This is the desired value of Looping
$count = 0; //First we set the count to be zeo
echo "<h2> Here goes the values</h2>";
foreach($my_array as $key => $val) {
    echo "The value of $key is $val<br/>"; //Print the value of the Array
    $count++; //Increase the value of the count by 1
    if($count==$max_loop) break; //Break the loop is count is equal to the max_loop
}
https://snipt.net/embed/d772bf9ea7a11808e574884f8251b543/
https://snipt.net/raw/d772bf9ea7a11808e574884f8251b543/
d772bf9ea7a11808e574884f8251b543
text
Text only
8
2014-04-21T21:20:11
True
False
Jul 08, 2012 at 11:08 AM
/api/public/snipt/43596/
break-a-loop-after-a-certain-number
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>$max_loop=5; //This is the desired value of Looping </span><span id="L-2"><a name="L-2"></a>$count = 0; //First we set the count to be zeo </span><span id="L-3"><a name="L-3"></a>echo &quot;&lt;h2&gt; Here goes the values&lt;/h2&gt;&quot;; </span><span id="L-4"><a name="L-4"></a>foreach($my_array as $key =&gt; $val) { </span><span id="L-5"><a name="L-5"></a> echo &quot;The value of $key is $val&lt;br/&gt;&quot;; //Print the value of the Array </span><span id="L-6"><a name="L-6"></a> $count++; //Increase the value of the count by 1 </span><span id="L-7"><a name="L-7"></a> if($count==$max_loop) break; //Break the loop is count is equal to the max_loop </span><span id="L-8"><a name="L-8"></a>} </span></pre></div> </td></tr></table>
break, loop, php
#17221

PHP

worrdpress loop

1
2
3
4
5
6
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
https://snipt.net/embed/b93e68b5b24c87df6c0fac9ec906d38f/
https://snipt.net/raw/b93e68b5b24c87df6c0fac9ec906d38f/
b93e68b5b24c87df6c0fac9ec906d38f
php
PHP
6
2014-04-21T22:37:59
True
False
/api/public/snipt/17221/
worrdpress-loop
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="k">if</span> <span class="p">(</span> <span class="nx">have_posts</span><span class="p">()</span> <span class="p">)</span> <span class="o">:</span> <span class="k">while</span> <span class="p">(</span> <span class="nx">have_posts</span><span class="p">()</span> <span class="p">)</span> <span class="o">:</span> <span class="nx">the_post</span><span class="p">();</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="cp">&lt;?php</span> <span class="k">endwhile</span><span class="p">;</span> <span class="k">else</span><span class="o">:</span> <span class="cp">?&gt;</span><span class="x"></span> </span><span id="L-5"><a name="L-5"></a><span class="x">&lt;p&gt;</span><span class="cp">&lt;?php</span> <span class="nx">_e</span><span class="p">(</span><span class="s1">&#39;Sorry, no posts matched your criteria.&#39;</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x">&lt;/p&gt;</span> </span><span id="L-6"><a name="L-6"></a><span class="cp">&lt;?php</span> <span class="k">endif</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
loop, php, wordpress