Troubleshooting.PHPSyntaxHighlight History

Show minor edits - Show changes to markup

June 28, 2008, at 14:43 UTC by Allan Odgaard - Add long explanation about using HTML for PHP files
Added lines 1-40:
  • Problem:** I switched the language type to PHP and now I get wrong syntax highlight (or similar).
  • Short answer:** Please switch back to using HTML as the language for your PHP files.
  • Long answer:** PHP is short for _PHP Hypertext Preprocessor_, i.e. it is a preprocessor, it is **not** meant as a standalone language.

Quoting [the manual][1]:

    > When PHP parses a file, it looks for opening and
    > closing tags, which tell PHP to start and stop
    > interpreting the code between them. Parsing in this
    > manner allows php to be embedded in all sorts of
    > different documents, as everything outside of a pair
    > of opening and closing tags is ignored by the PHP
    > parser. Most of the time you will see php embedded in
    > HTML documents […]

So a document like the following **is not PHP**:

    $var i = 42;

You **must** enclose that code in `<?php … ?>`. Even when you have a document like the following:

    <?php include '' ?>

The `` include file **is still HTML**, again [quoting the PHP manual][2]:

    > When a file is included, parsing drops out of PHP
    > mode and into HTML mode at the beginning of the
    > target file, and resumes again at the end. For this
    > reason, any code inside the target file which should
    > be executed as PHP code must be enclosed within valid
    > PHP start and end tags.

For this reason we designed the HTML grammar to handle `<?php … ?>` tags and correctly switch to PHP mode when these tags are seen, as they are mandatory, and almost always found within HTML documents (which embed PHP).

We did **not** design PHP to be used as a stand-alone language, because from the above quotes, it is **not** a standalone language. It is **always** embedded into something else, and 99% of the time, that _something else_ is HTML.

[1]: [2]: