PHP:如何跳过注释?

标签 php language-design comments

好吧,如果我评论某些内容,它在所有语言中都会被跳过,但它们是如何被跳过的以及阅读的内容是什么?

例子:

// 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/

相关文章:

scala - 是否计划在 Scala 的 future 版本中进行任何语言或规范更改?

php - 如何评论模板文件? (.tpl)

excel - 以编程方式将图片插入单元格注释中

php - Live Web Feed + Masonry 'Wall' with CMS

php - 如何转换yyyy-MM-ddTHH :mm:ssZ to yyyy-MM-dd HH:mm:ss?

java - 在 Java 上使用 DSL 的第一步?

f# - 为什么 F# 的 Collections.Seq 模块基本上重新实现了所有的 Enumerable 扩展方法?

javascript -/**/注释风格起源于什么语言?

php - 在 Woocommerce 中一次只允许购物车中的一个产品类别

php - mySQL - 匹配拉丁(英语)表单输入到 utf8(非英语)数据