user7351238
user7351238

Reputation:

Adding ?> to a PHP comment

I am fairly new to PHP and trying to create a piece of community learning code for my co-learners to help them and I want to put a coding tip in the comments. I tried writing the following:

# Tip -- when PHP is the only code in a file, don't put ?> at the end as this can cause bugs if white-space or additional PHP code is written to the end of the file after the file is initially created, after the ?>

However, typing ?> in the comment ends the file and the comment. How do I escape this? I tried using \ but that doesn't seem to work, and would also confuse the sense of the comment.

How is this done?

Upvotes: 2

Views: 65

Answers (1)

Spencer Wieczorek
Spencer Wieczorek

Reputation: 21575

PHP doesn't work well with single lined comments using the closing and starting tags. Although you can use a muti-lined comment:

/* Tip -- when PHP is the only code in a file, don't put ?> at the end as this can cause bugs if white-space or additional PHP code is written to the end of the file after the file is initially created, after the ?> */

Note this is mentioned in the Comment Documentation for PHP:

The "one-line" comment styles only comment to the end of the line or the current block of PHP code, whichever comes first. This means that HTML code after // ... ?> or # ... ?> WILL be printed: ?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that.

Upvotes: 2

Related Questions