# Counting rows, and the first and last

> Count rows and treat the first and last differently, with plain PHP.

Source: https://uglydev.com/recipes/counting-rows-and-the-first-and-last/
Updated: 19 November 2026

```php
<?php
$slides = ugly_rows( 'slides' );
$total  = count( $slides );
?>

<?php foreach ( $slides as $i => $slide ) : ?>
	<div class="slide<?php echo 0 === $i ? ' is-first' : ''; ?><?php echo $total - 1 === $i ? ' is-last' : ''; ?>">
		<?php $slide->show( 'title' ); ?>
	</div>
<?php endforeach; ?>
```

`ugly_rows()` gives a normal PHP list, so `count()`, keys and `array_slice()` work as you expect.
