Here i have a simple while loop to achieve this result:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 <— This is echoed out to the browser. Notice there is no comma after 10.
<?php
for ($count = 0; $count <= 10; $count++) {
echo $count;
if ($count == 10) {
break;
}
echo ', ';
}
?>
I want to achieve the same thing with ages.
I have a simple array here
<?php
$ages = array(2, 3, 5, 8, 12, 14, 18);
?>
I want the ages echoed out like so:
2, 3, 5, 8, 12, 14, 18 <---- Without the comma’s too
Here’s my code:
<?php
while ($age = current($ages)) {
echo $age;
// How do i know if the array pointer has reached the end
// So that i can stop it from putting the comma below this line..
echo ', ';
next($ages);
}
?>
How do i know if the array pointer has reached the end? So that i can stop it from putting the comma…