reference appendices
Parser Tokens
PHP exposes parser token constants for tools that inspect source code. Token streams are useful for lightweight analysis, while real refactoring tools should normally use a full parser that understands syntax structure.
Use This Reference When
- Building a source inspection script.
- Understanding output from
token_get_all(). - Choosing whether a task needs tokens or an abstract syntax tree.
Inspect Tokens
PHP example
<?php
$tokens = token_get_all('<?php echo "hi";');
foreach ($tokens as $token) {
echo is_array($token) ? token_name($token[0]) : $token;
echo PHP_EOL;
}
// Prints token names and punctuation.
Tokens retain lexical information such as whitespace and comments. They do not by themselves provide the richer relationships a parser library builds.
Practice
Inspect a Short PHP File
Run token_get_all() against a short PHP string and identify the token for echo, whitespace, and the inline string.
Show solution
Use token_name() for array-shaped tokens. Punctuation may appear as a one-character string rather than an array token.