You can exit a loop at any point within loop by using the break statement.
It exits the loop and moves to the first line of code outside the loop.
The following while loop counts to 10 and then exits the loop:
<?php $count = 0;/*ww w .java 2s . c o m*/ while ( true ) { $count++; echo "I've counted to: $count \n "; if ( $count == 10 ) break; } ?>