Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: How are comments skipped?

Well if I comment something it's skipped in all languages, but how are they skipped and what is readed?

Example:

// This is commented out

Now does PHP reads the whole comment to go to next lines or just reads the //?

like image 981
Adam Halasz Avatar asked Apr 07 '26 02:04

Adam Halasz


1 Answers

The script is parsed and split into tokens.

You can actually try this out yourself on any valid PHP source code using token_get_all(), it uses PHP's native tokenizer.

The example from the manual shows how a comment is dealt with:

<?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 */'));
?>
like image 159
Pekka Avatar answered Apr 09 '26 14:04

Pekka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!