好吧,如果我评论某些内容,它在所有语言中都会被跳过,但它们是如何被跳过的以及阅读的内容是什么?
例子:
// This is commented out
现在 PHP 是读取整个注释以转到下一行还是只读取 //
?
最佳答案
脚本被解析并拆分为标记。
您实际上可以使用 token_get_all()
在任何有效的 PHP 源代码上自己进行尝试,它使用 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 */'));
?>
关于PHP:如何跳过注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4359738/