Content in the head section??

T

Tim W

I have been looking through some free online php courses to learn a bit
of basic php. Here is a sample page of one of them

http://www.homeandlearn.co.uk/php/php4p6.html

You will see the author puts a print() command in the head section of
the html, to show effetively content on the html page. I was amazed it
worked. I have never seen visible content in the head of an html document.

I take it that this is completely bonkers and something which never
ought to be done, or is there some reason to put stuff there sometimes?
And why would a browser ever display any text from the head section?

Tim W
 
N

Norman Peelman

I have been looking through some free online php courses to learn a bit
of basic php. Here is a sample page of one of them

http://www.homeandlearn.co.uk/php/php4p6.html

You will see the author puts a print() command in the head section of
the html, to show effetively content on the html page. I was amazed it
worked. I have never seen visible content in the head of an html document.

I take it that this is completely bonkers and something which never
ought to be done, or is there some reason to put stuff there sometimes?
And why would a browser ever display any text from the head section?

Tim W

While it appears to work, it also appears to be a typo as in later
pages the PHP is inside the <body> element, not the <head>. That being
said, I am surprised that this also works (Google Chrome at least):

<HTML>
<HEAD>
<TITLE>HEADer test</TITLE>
<A ID=1 NAME=one HREF="blah">Header link</A><BR>
</HEAD>
<BODY>
Body text
</BODY>
</HTML>

I haven't investigated the validity of it nor can I think of a use
either.
 
J

Jukka K. Korpela

I am surprised that this also works (Google Chrome at least):

<HTML>
<HEAD>
<TITLE>HEADer test</TITLE>
<A ID=1 NAME=one HREF="blah">Header link</A><BR>
</HEAD>
<BODY>
Body text
</BODY>
</HTML>

I haven't investigated the validity of it nor can I think of a use
either.

It isn't valid of course. But it works, because the <A> element
implicitly terminates the open <HEAD> element. You can see this if you
inspect the page using the developer tools (F12): the Elements pane
shows how the <A> is being treated as the first child of <BODY>.

This doesn't have practical use. It just means that if you try to put
visible content inside <HEAD>, it will succeed, since it will really be
in the <BODY> element.

In fact, division of <HTML> into <HEAD> and <BODY> is rather pointless.
The division does not matter when the document is parsed and otherwise
processed. And the *tags* <head>, </head>, <body>, and </body> are
optional, except in XHTML, and even in real XHTML, they can be omitted
in practice (if you omit both the opening tag and the matching closing
tag), because it's just validity error (and browsers don't care about
validity), not a well-formedness error.

The division was probably introduced as "logical" in some people's
logic, or as an attempt at trying to tell that <STYLE>, <LINK>, <META>
etc. should appear at the start of a document rather than later. In
practice, apart from special cases like <META> specifying charset, those
tags can be scattered around - only HTML validators will care (and this
might matter, since validators can spot significant errors, too).
 
N

Norman Peelman

It isn't valid of course. But it works, because the <A> element
implicitly terminates the open <HEAD> element. You can see this if you
inspect the page using the developer tools (F12): the Elements pane
shows how the <A> is being treated as the first child of <BODY>.

Ahh yes, I see that now. It's simply been moved the <BODY>.
 
T

Tim W

Ahh yes, I see that now. It's simply been moved the <BODY>.
Although I can't see it has much to do with the <a> tag, even a bit of
text without tags in the head displays on the page in the body

Tim W
 
J

Jukka K. Korpela

Although I can't see it has much to do with the <a> tag, even a bit of
text without tags in the head displays on the page in the body

That's right. Any non-whitespace content also terminates the HEAD
element and starts a BODY element. The HEAD element may *only* contain
elements and whitespace.

This doesn't matter much, except that validation will get confused, and
this confuses the person trying to validate his pages. Oh, and it does
affect the DOM. But the elements will be otherwise processed the same
way, whether the browser things they are in the HEAD or in the BODY.
 
T

Tim W

That's right. Any non-whitespace content also terminates the HEAD
element and starts a BODY element. The HEAD element may *only* contain
elements and whitespace.

This doesn't matter much, except that validation will get confused, and
this confuses the person trying to validate his pages. Oh, and it does
affect the DOM. But the elements will be otherwise processed the same
way, whether the browser things they are in the HEAD or in the BODY.

Thanks. so it doesn't matter much in the context of that php tutorial
except that it would be a very bizarre way of actually doing a real
production page with a script on it.

TW
 
W

William Gill

I have been looking through some free online php courses to learn a bit
of basic php. Here is a sample page of one of them

http://www.homeandlearn.co.uk/php/php4p6.html

You will see the author puts a print() command in the head section of
the html, to show effetively content on the html page. I was amazed it
worked. I have never seen visible content in the head of an html document.

I take it that this is completely bonkers and something which never
ought to be done, or is there some reason to put stuff there sometimes?
And why would a browser ever display any text from the head section?

Tim W

Tim,

Without going on the tangent about the invalid anchor tag in the head
element, here is what's happening on the example given on the page you
provided;

Assume you have previously set $_POST['username'] to "Tim W":

<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$username = $_POST['username'];
print ($username);
?>
</head>

produces:

<html>
<head>
<title>A BASIC HTML FORM</title>
Tim W
</head>

examining the result, it is invalid.

However if the code were changed to:

<html>
<head>
<title>
<?PHP
$username = $_POST['username'];
print ($username);
?>
</title>
</head>

would produce:

<html>
<head>
<title>Tim W</title>
</head>

which is valid.

The inserted php snippets are run at the time the page is requested, and
if the snippet produces text output, it will be placed at that point in
the document, so if you tell php to type an invalid document it will.

On the other hand if the example were changed to:
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$username = $_POST['username'];
?>
</head>


the snippet would not return any output so the result would be:

<html>
<head>
<title>A BASIC HTML FORM</title>
</head>

but in the document the variable $username will contain whatever was in
the "superglobal" array variable $_POST indexed as 'username'.

All this means, you are right it is bonkers, and the example is flawed,
but what it demonstrates is php will let you do things you don't really
want to do if you are not careful.
 
N

Norman Peelman

I have been looking through some free online php courses to learn a bit
of basic php. Here is a sample page of one of them

http://www.homeandlearn.co.uk/php/php4p6.html

You will see the author puts a print() command in the head section of
the html, to show effetively content on the html page. I was amazed it
worked. I have never seen visible content in the head of an html
document.

I take it that this is completely bonkers and something which never
ought to be done, or is there some reason to put stuff there sometimes?
And why would a browser ever display any text from the head section?

Tim W

Tim,

Without going on the tangent about the invalid anchor tag in the head
element, here is what's happening on the example given on the page you
provided;

Assume you have previously set $_POST['username'] to "Tim W":

<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$username = $_POST['username'];
print ($username);
?>
</head>

produces:

<html>
<head>
<title>A BASIC HTML FORM</title>
Tim W
</head>

examining the result, it is invalid.

However if the code were changed to:

<html>
<head>
<title>
<?PHP
$username = $_POST['username'];
print ($username);
?>
</title>
</head>

would produce:

<html>
<head>
<title>Tim W</title>
</head>

which is valid.

The inserted php snippets are run at the time the page is requested, and
if the snippet produces text output, it will be placed at that point in
the document, so if you tell php to type an invalid document it will.

On the other hand if the example were changed to:
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
$username = $_POST['username'];
?>
</head>


the snippet would not return any output so the result would be:

<html>
<head>
<title>A BASIC HTML FORM</title>
</head>

but in the document the variable $username will contain whatever was in
the "superglobal" array variable $_POST indexed as 'username'.

All this means, you are right it is bonkers, and the example is flawed,
but what it demonstrates is php will let you do things you don't really
want to do if you are not careful.

This isn't really what the OP was questioning. That being said, PHP
isn't *letting* anything happen here. While PHP can tell the difference
when being called from the command line or web server, it doesn't know
where in the HTML output it is being called.
 
T

Tim Streater

Norman Peelman said:
On 10/16/2013 02:33 PM, William Gill wrote:

[much stuff snipped]

It's not bonkers at all, it's simply how it's supposed to work. The idea
is that you learn to do it right, just as with any programming language
or environment.
This isn't really what the OP was questioning. That being said, PHP
isn't *letting* anything happen here. While PHP can tell the difference
when being called from the command line or web server, it doesn't know
where in the HTML output it is being called.

The salient point here is that the *whole file* is being processed by
php. Simply that anything outside the <?php ?> tags is read by php and
sent straight to the output without modification. Anything *inside* is
treated as php code and executed. That may or may not result in more
text being sent to the output *at the point where that text is
generated* by, f'rinstance, a php echo statement.
 
D

Denis McMahon

Thanks. so it doesn't matter much in the context of that php tutorial
except that it would be a very bizarre way of actually doing a real
production page with a script on it.

Actually it matters very much, because it is from such tutorials that
some of the most pig headed and obstinate idiots on the internet get
their coding skills, and once they have latched onto a wrong thing, such
as interleaved tags are OK, they never give up on that thing.
 
T

Tim W

Actually it matters very much, because it is from such tutorials that
some of the most pig headed and obstinate idiots on the internet get
their coding skills, and once they have latched onto a wrong thing, such
as interleaved tags are OK, they never give up on that thing.

Yeah, the kinds of people who stand on right on escalators and don't
rewind the VHS tape before returning it to the shop.

tim W
 
W

William Gill

This isn't really what the OP was questioning.That being said, PHP
isn't *letting* anything happen here. While PHP can tell the difference
when being called from the command line or web server, it doesn't know
where in the HTML output it is being called.
I was a little unsure myself, if he was more concerned about the PHP
placing content in an invalid location, or the author of the tutorial
coding erroneous HTML, but since he said he was very new to PHP I tried
to clarify what PHP did and what the author did.

Yes, PHP will "let" you do a lot of things that don't violate PHP, but
may violate the rules of the end user of its output, in this case HTML.

"PHP is a widely-used general-purpose scripting language..." which means
output can be used in any number of applications. I frequently use it
to process text having nothing to do with HTML. The point being, PHP
validates against its own syntax, but not HTML. Just because it is
frequently used in conjunction with HTML, or more specifically to
produce HTML documents, don't ask PHP to do something you shouldn't,
because it will. It's up to the driver to keep the car out of the ditch,
because it will "let" you if you ask.
 
T

Tim W

I was a little unsure myself, if he was more concerned about the PHP
placing content in an invalid location, or the author of the tutorial
coding erroneous HTML, but since he said he was very new to PHP I tried
to clarify what PHP did and what the author did.

Yes, PHP will "let" you do a lot of things that don't violate PHP, but
may violate the rules of the end user of its output, in this case HTML.

"PHP is a widely-used general-purpose scripting language..." which means
output can be used in any number of applications. I frequently use it
to process text having nothing to do with HTML. The point being, PHP
validates against its own syntax, but not HTML. Just because it is
frequently used in conjunction with HTML, or more specifically to
produce HTML documents, don't ask PHP to do something you shouldn't,
because it will. It's up to the driver to keep the car out of the ditch,
because it will "let" you if you ask.
thanks, I understand all that. It wasn't a php question, but an html
question, because I had never seen anyone put content in the head
section, was suprised it was even displayed and wondered if there could
ever be a good reason for it.

Tim w
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top