Log in

View Full Version : display correctly in view source



mtran
04-27-2007, 02:26 PM
I created a template page which will be included in every page of the site.

<?php
echo '<html>';
echo '<head>';
echo '<title>'.$title.'</title>';..............
?>
My question is when I view source of the individual pages - everything is displayed in a long string like this:

<html><head><title>.....</title>...................................
How to make it displayed normally with line breaks - just the code in view source, not the actual content.
<html>
<head>
<title>...

I looks easy but I don't know. Thanks.

jr_yeo
04-27-2007, 02:36 PM
<?php
echo '<html>' . "\n";
?>

boogyman
04-27-2007, 02:51 PM
<?php
echo '<html>' . "\n";
?>



\n = inserts a newline "<br />"
\t = inserts a tab
\r = inserts a carriage return

Twey
04-27-2007, 05:08 PM
When echoing chunks of HTML, though, it's easier to break out of PHP parsing mode:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html/strict.dtd">
<html>
<head>
<title><?php print $title; ?></title>
</head>
<body>

djr33
04-27-2007, 07:35 PM
Or, you can actually include that type of character in the PHP code:
<?php
echo "This has a
line break";

//or

echo <<<EOF
This text now can be formatted
however you want
include any symbols.. whatever
and variables like {$var}.
Just end like:
EOF;

?>

The right method to use depends on the circumstances. Twey's idea of breaking out of the PHP makes a lot of sense sometimes, too.
You can expand on that too:
<?php
if (1==1) { ?>
This is just some html that will
only be output if the IF statement returns true
<?php
}
//now the PHP can continue
?>