John Chrysostom
John Chrysostom

Reputation: 3983

PHP Losing Characters on Echo with Angle Bracket

I have built a PHP function to echo some table output to a webpage.

The code to produce a header row is here:

echo('<tr><th class="leftcolumn">'.$headers[0].'</th>');

for ($i=1; $i<count($headers); $i++) {
    echo('<th class="normalcolumn">'.$headers[$i].'</th>');
}

echo("</tr>");

The output I am getting from this quite frequently has truncated close tags... Such as the following:

<th class="normalcolumn">Employed<br />After 2 Yrs/th>

I am at a loss as to what's causing this behavior. So, just to test things, I created the following simple script:

<?php
echo("After 2 Yrs"."</th");
?>

The HTML output I get from this (yes, in the view source page) is as follows:

After 2 Yrs

The extra text is notably absent.

Any ideas what the problem may be?

Upvotes: 2

Views: 483

Answers (1)

PlantTheIdea
PlantTheIdea

Reputation: 16369

Why are you echoing HTML?

<tr>
    <th class="leftcolumn"><?php echo $headers[0]; ?></th>
    <?php
        for ($i=1; $i<count($headers); $i++) {
    ?>

    <th class="normalcolumn">
        <?php echo $headers[$i]; ?>
    </th>

    <?php
        }
    ?>
</tr>

This way you don't need to worry about PHP having issues with HTML characters.

Upvotes: 1

Related Questions