3v4l.org

run code in 300+ PHP versions simultaneously
<?php function x_the_image( $post_id ) { $attachment_ids = array( 1, 2, 3, 4 ); $next_id = -1; foreach ( $attachment_ids as $idx => $attachment_id ) { if ( $attachment_id == $post_id ) { $next_id = $attachment_ids[ ( $idx + 1 ) % count( $attachment_ids ) ]; break; } } return $next_id; } var_dump( x_the_image(2) === 3, x_the_image(3) === 4, x_the_image(4) === 1, x_the_image(5) );

preferences:
72.32 ms | 404 KiB | 5 Q