What is the purpose of token_get_all in PHP?
The purpose of `token_get_all` in PHP is to tokenize a PHP source code file, breaking it down into individual tokens such as keywords, variables, strings, and operators. This function is commonly used for tasks such as code analysis, syntax highlighting, and code manipulation.
$code = '<?php echo "Hello, world!"; ?>';
$tokens = token_get_all($code);
foreach ($tokens as $token) {
if (is_array($token)) {
echo "Token: " . token_name($token[0]) . ", Value: " . $token[1] . PHP_EOL;
} else {
echo "Token: " . $token . PHP_EOL;
}
}