In what situations is the strtok function in PHP useful for string manipulation, and what are some common mistakes to avoid when using it?

The strtok function in PHP is useful for breaking a string into smaller parts based on a specific delimiter. It can be handy for tasks like parsing CSV files, extracting words from a sentence, or splitting a URL into its components. However, common mistakes when using strtok include not properly handling cases where the delimiter is not found, not resetting the pointer between calls, and using it in a loop without checking for false return values.

// Example of using strtok to split a string by commas and output each part
$string = "apple,banana,cherry";
$delimiter = ",";
$token = strtok($string, $delimiter);

while ($token !== false) {
    echo $token . "<br>";
    $token = strtok($delimiter);
}