2

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?

2
  • 3
    Try to use multi line comment like /* */ Commented May 12, 2017 at 14:23
  • I'll give it a shot... Commented May 12, 2017 at 14:24

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.