Forum Moderators: coopster

Message Too Old, No Replies

adding / to HTML tags

         

sssweb

9:44 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



I've seen that when using some HTML tags in a php echo statement that you should add a space, then a forward slash before the closing tag. For example:

<br> becomes <br />

Can someone tell me the reason this is needed, and whether it applies to ALL HTML tags? For example, what about these:

<a href="page.htm" />link</a />
<font class="big" />text</font />
<form />
<input type="submit" value="Pay" name="submit" />
</form />

When (and why) is it needed, and when (if ever, not)?

PHP_Chimp

9:56 pm on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It depends on if you are using html or xhtml. As xhtml is XML all tags need to be closed. Some are closed with an end of tag i.e. <form action=bla bla>form code</form> others are self closing i.e. <hr />

So the /> is just the end of the tag. If you are using an HTML doctype then you shouldnt use /> as this in invalid, however if you are using XHTML then you will get validation errors if you dont close the tags properly.

There should be a space after the final letter in the tag then /> as some browsers choke on <br/> all are fine with <br />, but only if you are using XHTML.

sssweb

10:07 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



So php echoes within a straight HTML doc shouldn't use <br />?

A lot of my code has that and it seems to work properly. Am I just lucky...so far?!

This is my doc header; I assume this is HTML, not XML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

PHP_Chimp

10:16 pm on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HTML shouldnt use <br /> or any other self closing tags. However as HTML is not XML there is no real issue with using self closing tags (XML is not forgiving of any errors, it chokes and dies...although browsers have built in error compensation as most designers seem not to be bothered about XML); browsers will still do what you want, so not a problem.
If you try to validate the document then it will fail the validation as HTML doesnt have self closing tags, but will all visual browsers not having any problems (im not sure about other types, but guess that they wont have any problems either) then I wouldn't worry about it.

That is an HTML doctype so you are fine.

sssweb

10:21 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



Great, thanks.