Show first n elements of array, and count elements not shown This code mimics the "likes" tooltip from Facebook. 

Given an array with the list of users that liked the content, it shows the first n users and displays a final item with the count of the users not shown.

It works as expected, but I'm sure it could be better written.

&lt;?  if ($count = $this-&gt;likes-&gt;count()) { 
        $visible = 12;
        $hidden = $count &gt; $visible ? $count - $visible : 0;
?&gt;
        &lt;div class="likes_detail"&gt;
&lt;?      $r = 0;
        foreach ($this-&gt;likes as $like) {
?&gt;
            &lt;span&gt;&lt;?= $like-&gt;name ?&gt;&lt;/span&gt;
&lt;?          $r++;
            if ($r &gt;= $visible) { break; };
        }
        if ($count &gt; $visible) { 
?&gt;
            &lt;span class="more_users"&gt;and &lt;?= $hidden ?&gt; more&amp;hellip;&lt;/span&gt;
&lt;?      } ?&gt;
        &lt;/div&gt;
&lt;?  } ?&gt;


Edit: Removed revised code after reading this meta post.
&lt;?  if ($count = $this-&gt;likes-&gt;count()) { 



It is more common to use the long form of the opening tag.  The second tag is only optionally supported by PHP installations.  Also, it can suddenly stop working if your host adjusts the configuration (possibly accidentally during an upgrade).  

&lt;?php
    if ($count = $this-&gt;likes-&gt;count()) {


It's also more common to put the code on a new line after the opening tag.  Unless you're opening and closing the PHP block on the same line.  

If you're interested, there is a PHP Coding Standard.  Personally I don't always follow it, but I find it a good way to make decisions on issues that aren't important to me.  It also makes your code more portable.  


            if ($r &gt;= $visible) { break; };



I'd break this up into multiple lines for readability.  

            if ($r &gt;= $visible) {
                break;
            }


The final semi-colon is unnecessary there.  It doesn't harm anything (perhaps a wasted clock cycle on the null instruction), but neither does it contribute anything.  


        if ($count &gt; $visible) { 



I might write this as 

        if ( $hidden &gt; 0 ) { 


The reason being that you then go on to output $hidden.  You also might consider moving the definition of $hidden to be directly before this if statement.  That puts all the code related to $hidden together and makes it easier to follow the logic.  

Alternately, you could stay with the original if and replace $hidden with $count - $visibility.  Then you don't need to define $hidden at all.