User Contributed Notes 9 notes

When the documentation says that the PHP parser ignores everything outside the <?php ... ?> tags, it means literally EVERYTHING. Including things you normally wouldn't consider "valid", such as the following:

<html><body><p<?php if ($highlight): ?> class="highlight"<?php endif;?>>This is a paragraph.</p></body></html>

Notice how the PHP code is embedded in the middle of an HTML opening tag. The PHP parser doesn't care that it's in the middle of an opening tag, and doesn't require that it be closed. It also doesn't care that after the closing ?> tag is the end of the HTML opening tag. So, if $highlight is true, then the output will be:

<html><body><p class="highlight">This is a paragraph.</p></body></html>

Otherwise, it will be:

<html><body><p>This is a paragraph.</p></body></html>

Using this method, you can have HTML tags with optional attributes, depending on some PHP condition. Extremely flexible and useful!

One aspect of PHP that you need to be careful of, is that ?> will drop you out of PHP code and into HTML even if it appears inside a // comment. (This does not apply to /* */ comments.) This can lead to unexpected results. For example, take this line:

<?php $file_contents = '<?php die(); ?>' . "\n";?>

If you try to remove it by turning it into a comment, you get this:

<?php// $file_contents = '<?php die(); ?>' . "\n";?>

Which results in ' . "\n"; (and whatever is in the lines following it) to be output to your HTML page.

The cure is to either comment it out using /* */ tags, or re-write the line as:

The German version of this documentation page is incorrect. I didn't understand what it wanted to tell me, so I went to the English version, which is clear. The Problem is that the section "dies arbeitet wie erwartet, da PHP, wenn..." follows the advanced escaping example in the German version, whereas it precedes the example in the English version. The English version then has its own (proper) explanation of the example, whereas the German version has none.

<script /> tags are a waste of time and simply inefficient in some simple cases :<body> <p style="color: <script language="php"> echo $text_color </script>;"> (...) VERY long text (...) </p></body>To render this example in a basic XHTML editor, you need to "echo()" all the content or break the XML rules.

The solution seems obvious to me : Why not add the shortcut "<?php= ?>" to be used within XML and XHTML documents ?<?php='example1'?><?php=$example2?>

These methods are just messy. Short-opening tags and ASP-styled tags are not always enabled on servers. The <script language="php"></script> alternative is just out there. You should just use the traditional tag opening:

Lead to something along the lines of messy code. Writing your application like this can just prove to be more of an inconvenience when it comes to maintenance.

If you have to deal chunks of HTML, then consider having a templating system do the job for you. It is a poor idea to rely on the coding islands method as a template system in any way, and for reasons listed above.