How can PHP developers effectively handle comments in their code using token_get_all?

When using token_get_all in PHP to analyze code, developers may encounter issues when trying to handle comments in the code. To effectively handle comments, developers can check the token type of each token returned by token_get_all and ignore tokens that represent comments (T_COMMENT and T_DOC_COMMENT).

$code = '<?php // This is a comment
echo "Hello, World!";';

$tokens = token_get_all($code);

foreach ($tokens as $token) {
    if (is_array($token)) {
        list($id, $text) = $token;
        
        if ($id !== T_COMMENT && $id !== T_DOC_COMMENT) {
            echo $text;
        }
    } else {
        echo $token;
    }
}