Example 1 — Loop Through Words
The classic pattern: call once with the string, then keep calling with the delimiter.
<?php
$text = "red green blue";
$tok = strtok($text, " ");
while ($tok !== false) {
echo $tok, PHP_EOL;
$tok = strtok(" ");
} How It Works
- Start tokenizing.
strtok($text, " ")returns the first word. - Check for false. The loop runs while a token exists.
- Ask for the next token.
strtok(" ")needs no string this time. - Stop at the end. When pieces run out, strtok() returns
false.

