Skip to content

PHP: Escaping from HTML Traditional Cache

This cache has been archived.

BlueRajah: I am archiving this cache to keep it from continually showing up in search lists, and to prevent it from blocking other cache placements. The reviewers requested the cache to be reviewed and no action was taken. If this was done in error please contact me immediately. If the cache was archived because of maintenance issues (no response to our earlier notes on the cache page) you will need to submit a new cache.

Thank you,
BlueRajah
Volunteer Geocache Reviewer - Utah

More
Hidden : 8/22/2010
Difficulty:
1.5 out of 5
Terrain:
1.5 out of 5

Size: Size:   micro (micro)

Join now to view geocache location details. It's free!

Watch

How Geocaching Works

Please note Use of geocaching.com services is subject to the terms and conditions in our disclaimer.

Geocache Description:


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, as in this example.

<p>This is going to be ignored.</p>
<?php echo 'While this is going to be parsed.'; ?>
<p>This will also be ignored.</p>

You can also use more advanced structures:

Example #1 Advanced escaping
<?php
if ($expression) {
?>
<strong>This is true.</strong>
<?php
} else {
?>
<strong>This is false.</strong>
<?php
}
?>

This works as expected, because when PHP hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation ) until it hits another opening tag. The example given here is contrived, of course, but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print().

There are four different pairs of opening and closing tags which can be used in PHP. Two of those, <?php ?> and <script language="php"> </script>, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.

Note:

Also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?> tags to remain compliant with standards.

Example #2 PHP Opening and Closing Tags
1. <?php echo 'if you want to serve XHTML or XML documents, do it like this'; ?>

2. <script language="php">
echo 'some editors (like FrontPage) don\'t
like processing instructions';
</script>

3. <? echo 'this is the simplest, an SGML processing instruction'; ?>
<?= expression ?> This is a shortcut for "<? echo expression ?>"

4. <% echo 'You may optionally use ASP-style tags'; %>
<%= $variable; # This is a shortcut for "<% echo . . ." %>

While the tags seen in examples one and two are both always available, example one is the most commonly used, and recommended, of the two.

Additional Hints (No hints available.)