467,910 Members | 1,578 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,910 developers. It's quick & easy.

Outputting formatted html from php scripts

Do people generally try and output nicely formatted html from their
scripts?

I have been trying to, with variable indenting for tables etc. It
makes it easier to see the html and perhaps debug it, but it can make
ugly looking scripts.

What's the general consensus?

Craig

Jul 17 '05 #1
  • viewed: 1908
Share:
12 Replies
I do for the most part, but I have never had an ugly script becouse of it.

I do not indent my tables too far in, some times I will restart the nesting
at colom 0 for a table.

--
Mike Bradley
http://www.gzentools.com -- free online php tools
"Craig Thomson" <cr***@spam.free> wrote in message
news:76********************************@4ax.com...
Do people generally try and output nicely formatted html from their
scripts?

I have been trying to, with variable indenting for tables etc. It
makes it easier to see the html and perhaps debug it, but it can make
ugly looking scripts.

What's the general consensus?

Craig

Jul 17 '05 #2
sorry, I hit send too fast, here is the rest of my post.

sometimes my tables outout like this:

<table>
<tr>
<td>

<table>
<tr>
<td>stuff</td>
</tr>
</table>

<td>
</tr>
</table>

--
Mike Bradley
http://www.gzentools.com -- free online php tools
"Craig Thomson" <cr***@spam.free> wrote in message
news:76********************************@4ax.com...
Do people generally try and output nicely formatted html from their
scripts?

I have been trying to, with variable indenting for tables etc. It
makes it easier to see the html and perhaps debug it, but it can make
ugly looking scripts.

What's the general consensus?

Craig

Jul 17 '05 #3
Craig Thomson wrote:
Do people generally try and output nicely formatted html from their
scripts?

I have been trying to, with variable indenting for tables etc. It
makes it easier to see the html and perhaps debug it, but it can make
ugly looking scripts.

What's the general consensus?


I don't :)

I might put a few "\n" here and there (usually after </tr> or <br/>) but
no indenting for the next line. If I want to look at the HTML produced I
tidy it first with
HTML Tidy ( @ http://www.w3.org/People/Raggett/tidy/ )
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #4
On Mon, 16 Feb 2004 05:47:05 GMT, Craig Thomson <cr***@spam.free>
wrote:
Do people generally try and output nicely formatted html from their
scripts?

I have been trying to, with variable indenting for tables etc. It
makes it easier to see the html and perhaps debug it, but it can make
ugly looking scripts.

What's the general consensus?


Personally, I don't bother.

My PHP code is nicely indented, so debugging is easy from the server
end. Also, I try and output fairly clean HTML (no nested tables, etc)
which makes dubugging from the client end easier.

I don't see the point in cluttering up one's script with \n's and
\t's. The browser doesn't need them and it saves a few bytes bandwidth
;-)

Here's a tip: Single quotes can be used in HTML for attributes, so you
can write:

print("<img src='whatever.png' alt='Whatever'>"); // nice.

as opposed to:

print("<img src=\"whatever.png\" alt=\"Whatever\">"); // ugly.

--
David ( @priz.co.uk )
Jul 17 '05 #5
David Mackenzie writes:
Here's a tip: Single quotes can be used in HTML for attributes, so you
can write: print("<img src='whatever.png' alt='Whatever'>"); // nice. as opposed to: print("<img src=\"whatever.png\" alt=\"Whatever\">"); // ugly.


Good tip. Here's another one. If ?> is followed immediately by a
newline then the newline will not be output. For example:

<?php echo "cat"; ?>
fish

Will write this:

catfish

If you follow the end-of-line ?> with a space you get this:

cat
fish

--

__o Alex Farran
_`\<,_ Analyst / Programmer
(_)/ (_) www.alexfarran.com

Jul 17 '05 #6
David Mackenzie wrote:
Here's a tip: Single quotes can be used in HTML for attributes, so you
can write:

print("<img src='whatever.png' alt='Whatever'>"); // nice.


Why use double quotes for PHP at all?

print('<img src="whatever.png" alt="whatever">'); // nicer IMHO
though, if I'm not using the return value of print() i prefer echo()
using the parenthesis-less syntax

echo '<img src="whatever.png" alt="whatever">';
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #7
On 16 Feb 2004 12:19:23 GMT, Pedro Graca <he****@hotpop.com> wrote:
David Mackenzie wrote:
Here's a tip: Single quotes can be used in HTML for attributes, so you
can write:

print("<img src='whatever.png' alt='Whatever'>"); // nice.
Why use double quotes for PHP at all?


I'm used to using double quotes for string literals from other
languages.
print('<img src="whatever.png" alt="whatever">'); // nicer IMHO


They're both preferable to \"whatever\".

--
David ( @priz.co.uk )
Jul 17 '05 #8
Pedro Graca <he****@hotpop.com> wrote in news:c0qcgb$19rnoe$1@ID-
203069.news.uni-berlin.de:
David Mackenzie wrote:
Here's a tip: Single quotes can be used in HTML for attributes, so you
can write:

print("<img src='whatever.png' alt='Whatever'>"); // nice.


Why use double quotes for PHP at all?

print('<img src="whatever.png" alt="whatever">'); // nicer IMHO


But that won't work if you need to interpolate the values of variables into
the generated output.
Jul 17 '05 #9
Eric Bohlman wrote:
Pedro Graca <he****@hotpop.com> wrote in news:c0qcgb$19rnoe$1@ID-
203069.news.uni-berlin.de:
David Mackenzie wrote:
Here's a tip: Single quotes can be used in HTML for attributes, so you
can write:

print("<img src='whatever.png' alt='Whatever'>"); // nice.


Why use double quotes for PHP at all?

print('<img src="whatever.png" alt="whatever">'); // nicer IMHO


But that won't work if you need to interpolate the values of variables into
the generated output.


No, it will not. But I prefer to not interpolate

<?php
$oldname = 'Pedro Graca';
echo '<input type="text" name="username" value="', $oldname, '"/>';
?>

is how I do it.
I agree that

<?php
$oldname = 'Pedro Graca';
echo "<input type='name' name='username' value='$oldname'/>";
?>

looks more like HTML than my preferred version, but I find it more
difficult to know what is being printed when interpolation is used.
I don't care for HTML looking scripts :)

One of the first things I do to scripts of other people is
"deinterpolate" it :)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #10
I use a composition of objects to produce the output, usually in
combination with files that are included by the objects.

The included files contain most of the lay out and are mostly in html
mode. I usually keep the html in each file properly formatted to make it
readable. I do not bother about the formatting of the final output that
is produced by the objects together. I don't want to becuase that would
make it harder to reuse the objects in whatever combination i like.

If i need to see the overall object composition structure in the output,
i switch on the object's debugging mode so that the start and end of the
output of each composing object is announced in html comments.

Greetings,

Henk Verhoeven.

Craig Thomson wrote:
Do people generally try and output nicely formatted html from their
scripts?

I have been trying to, with variable indenting for tables etc. It
makes it easier to see the html and perhaps debug it, but it can make
ugly looking scripts.

What's the general consensus?

Craig


Jul 17 '05 #11
For a while I try to use heredoc, but soon gave up. Nowaday I do a lot of
<?=$var?> and <? foreach():?>...<? endforeach; ?>. Makes fixing HTML layout
issues a little easier than doing echos.

My HTML forms tend to look like this:

<input name="hello" value="<? echo htmlspecialchars($hello); ?>">

<? $checked = araay($which => "checked"); ?>
<input name="which" type="radio" value="a" <?=$checked['a']?>>
<input name="which" type="radio" value="b" <?=$checked['b']?>>
<input name="which" type="radio" value="c" <?=$checked['c']?>>

<? $selected = array($cow => "selected"); ?>
<select name="cow">
<option value="3" <?$selected[3]?>>Mad</option>
<option value="4" <?$selected[4]?>>Dead</option>
<option value="5" <?$selected[5]?>>Tastes like chicken</option>
</select>

Uzytkownik "Craig Thomson" <cr***@spam.free> napisal w wiadomosci
news:76********************************@4ax.com...
Do people generally try and output nicely formatted html from their
scripts?

I have been trying to, with variable indenting for tables etc. It
makes it easier to see the html and perhaps debug it, but it can make
ugly looking scripts.

What's the general consensus?

Craig

Jul 17 '05 #12
Eric Bohlman wrote:
Pedro Graca <he****@hotpop.com> wrote in news:c0qcgb$19rnoe$1@ID-
203069.news.uni-berlin.de:

David Mackenzie wrote:
Here's a tip: Single quotes can be used in HTML for attributes, so you
can write:

print("<img src='whatever.png' alt='Whatever'>"); // nice.


Why use double quotes for PHP at all?

print('<img src="whatever.png" alt="whatever">'); // nicer IMHO

But that won't work if you need to interpolate the values of variables into
the generated output.


print "<img src=\"whatever.jpg\" alt=\"$description\">\n";

no problemo.

/m

Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by kingofkolt | last post: by
5 posts views Thread by Paul Lamonby | last post: by
1 post views Thread by Robert V | last post: by
9 posts views Thread by Nick Forrington | last post: by
3 posts views Thread by Craig Petrie | last post: by
1 post views Thread by robbiesmith79 | last post: by
4 posts views Thread by cybervigilante | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.