From: Andy McKenzie on
Hey folks,

I have the feeling this is a stupid question, but I can't even find
anything about it. Maybe I'm just not searching for the right things.

Here's the problem. I'm writing a lot of pages, and I hate going in
and out of PHP. At the same time, I want my HTML to be legible. When
you look at it, that's kind of a problem, though... for instance
(assume this had some PHP in the middle, and there was actually a
reason not to just put this in HTML in the first place):

Simple PHP:
<?php

echo '<html>';
echo '<head>';
echo ' <title>Page Title</title>';
echo '</head>';
echo '<body>';
echo '<p>This is the page body</p>';
echo '</body>';
echo '</html>';

?>


Output page source:
<html><head> <title>Page Title</title></head><body><p>This is the
page body</p></body></html>


Now, I can go through and add a newline to the end of each line (echo
'<html>' . "\n"; and so on), but it adds a lot of typing. Is there a
way to make this happen automatically? I thought about just building
a simple function, but I run into problem with quotes -- either I
can't use single quotes, or I can't use double quotes. Historically,
I've dealt with the issue by just having ugly output code, but I'd
like to stop doing that. How do other people deal with this?

Thanks,
Alex
From: TR Shaw on

On Sep 20, 2010, at 2:56 PM, Andy McKenzie wrote:

> Hey folks,
>
> I have the feeling this is a stupid question, but I can't even find
> anything about it. Maybe I'm just not searching for the right things.
>
> Here's the problem. I'm writing a lot of pages, and I hate going in
> and out of PHP. At the same time, I want my HTML to be legible. When
> you look at it, that's kind of a problem, though... for instance
> (assume this had some PHP in the middle, and there was actually a
> reason not to just put this in HTML in the first place):
>
> Simple PHP:
> <?php
>
> echo '<html>';
> echo '<head>';
> echo ' <title>Page Title</title>';
> echo '</head>';
> echo '<body>';
> echo '<p>This is the page body</p>';
> echo '</body>';
> echo '</html>';
>
> ?>
>
>
> Output page source:
> <html><head> <title>Page Title</title></head><body><p>This is the
> page body</p></body></html>
>
>
> Now, I can go through and add a newline to the end of each line (echo
> '<html>' . "\n"; and so on), but it adds a lot of typing. Is there a
> way to make this happen automatically? I thought about just building
> a simple function, but I run into problem with quotes -- either I
> can't use single quotes, or I can't use double quotes. Historically,
> I've dealt with the issue by just having ugly output code, but I'd
> like to stop doing that. How do other people deal with this?
>
> Thanks,
> Alex

Alex

Just add a \n at the end as

echo '<html>\n';

Of course if your on windows you might want to output \r\n

Tom
From: Marc Guay on
What sort of results do you get with

echo "

<html>
<head>
<title>Page Title</title>
</head>
<body>
<p>This is the page body</p>
</body>
</html>

";

or similar?
From: "=?utf-8?B?YXNoQGFzaGxleXNoZXJpZGFuLmNvLnVr?=" on
Have you looked at heredoc and nowdoc in the manual? They could be what you're after, as they preserve the output in the way you write it.

Thanks,
Ash
http://www.ashleysheridan.co.uk

----- Reply message -----
From: "Andy McKenzie" <amckenzie4(a)gmail.com>
Date: Mon, Sep 20, 2010 19:56
Subject: [PHP] Auto-generating HTML
To: "PHP General list" <php-general(a)lists.php.net>

Hey folks,

I have the feeling this is a stupid question, but I can't even find
anything about it. Maybe I'm just not searching for the right things.

Here's the problem. I'm writing a lot of pages, and I hate going in
and out of PHP. At the same time, I want my HTML to be legible. When
you look at it, that's kind of a problem, though... for instance
(assume this had some PHP in the middle, and there was actually a
reason not to just put this in HTML in the first place):

Simple PHP:
<?php

echo '<html>';
echo '<head>';
echo ' <title>Page Title</title>';
echo '</head>';
echo '<body>';
echo '<p>This is the page body</p>';
echo '</body>';
echo '</html>';

?>


Output page source:
<html><head> <title>Page Title</title></head><body><p>This is the
page body</p></body></html>


Now, I can go through and add a newline to the end of each line (echo
'<html>' . "\n"; and so on), but it adds a lot of typing. Is there a
way to make this happen automatically? I thought about just building
a simple function, but I run into problem with quotes -- either I
can't use single quotes, or I can't use double quotes. Historically,
I've dealt with the issue by just having ugly output code, but I'd
like to stop doing that. How do other people deal with this?

Thanks,
Alex

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

From: Paul M Foster on
On Mon, Sep 20, 2010 at 02:56:47PM -0400, Andy McKenzie wrote:

> Hey folks,
>
> I have the feeling this is a stupid question, but I can't even find
> anything about it. Maybe I'm just not searching for the right things.
>
> Here's the problem. I'm writing a lot of pages, and I hate going in
> and out of PHP. At the same time, I want my HTML to be legible. When
> you look at it, that's kind of a problem, though... for instance
> (assume this had some PHP in the middle, and there was actually a
> reason not to just put this in HTML in the first place):
>
> Simple PHP:
> <?php
>
> echo '<html>';
> echo '<head>';
> echo ' <title>Page Title</title>';
> echo '</head>';
> echo '<body>';
> echo '<p>This is the page body</p>';
> echo '</body>';
> echo '</html>';
>
> ?>
>
>
> Output page source:
> <html><head> <title>Page Title</title></head><body><p>This is the
> page body</p></body></html>
>
>
> Now, I can go through and add a newline to the end of each line (echo
> '<html>' . "\n"; and so on), but it adds a lot of typing. Is there a
> way to make this happen automatically? I thought about just building
> a simple function, but I run into problem with quotes -- either I
> can't use single quotes, or I can't use double quotes. Historically,
> I've dealt with the issue by just having ugly output code, but I'd
> like to stop doing that. How do other people deal with this?
>
> Thanks,
> Alex

The *real* way to do this is not to echo all that HTML. Just type the
HTML the way you want it. Where you have PHP embedded in it, just
surround it with the appropriate <?php and ?>. This business of echoing
HTML is silly. Do this:

<html>
<head>
<title>Page Title</title>
</head>
<body>
<?php echo $hello_world; ?>
<p>This is the page body.</p.
</body>
</html>

The $hello_world variable is defined elsewhere, like the file which
"include"s this one. Call your file 'pagename.php' and the server will
catch whatever PHP is embedded in it and pass on the HTML.

Paul

--
Paul M. Foster
 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: PHP Email Question
Next: not able to connect to MySQL