php count the number of strings after exploded

<?php

$string = 'a|b|c|d|e|f';

$tags = explode('|' , $string);


foreach($tags as $i =>$key) {

    echo $i.' '.$key .'</br>';

}

?>

Try using:

echo count($tags); // Output of 6

Arrays start with a key of 0, not one. So when using anything else apart from count, you will constantly get 1 less than your expected (unless you modify the array prior to counting)


<?php

$string = 'a|b|c|d|e|f';

$tags = explode('|' , $string);

$count =count($tags);
  echo 'Count is: '.$count .'</br>';
$i = 1 ;
foreach($tags as $key) {

    echo $i.' '.$key .'</br>';
$i++;
}

?>

If you just need the total number, you could do this:

$tags = explode('|' , $string);
$num_tags = count($tags);