PHP:如何跳过评论?

Ada*_*dam 3 php comments language-design code-comments

好吧,如果我发表评论,它会在所有语言中被忽略,但是它们是如何被跳过的?

例:

// This is commented out
Run Code Online (Sandbox Code Playgroud)

PHP现在读取整个注释以转到下一行还是只读取//

Pek*_*ica 7

该脚本被解析并拆分为令牌.

您可以使用任何有效的PHP源代码自己尝试使用token_get_all()它,它使用PHP的本机标记器.

手册中的示例显示了如何处理注释:

<?php
$tokens = token_get_all('<?php echo; ?>'); /* => array(
                                                  array(T_OPEN_TAG, '<?php'), 
                                                  array(T_ECHO, 'echo'),
                                                  ';',
                                                  array(T_CLOSE_TAG, '?>') ); */

/* Note in the following example that the string is parsed as T_INLINE_HTML
   rather than the otherwise expected T_COMMENT (T_ML_COMMENT in PHP <5).
   This is because no open/close tags were used in the "code" provided.
   This would be equivalent to putting a comment outside of <?php ?> 
   tags in a normal file. */

$tokens = token_get_all('/* comment */'); 
// => array(array(T_INLINE_HTML, '/* comment */'));
?>
Run Code Online (Sandbox Code Playgroud)