By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

Why does include file text need to be bracketed by <?php> ... <?>

P: n/a
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>

One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?

Thanks,
M. McDonnell

May 28 '07 #1
Share this Question
Share on Google+
14 Replies


P: n/a

"Michael" <Mi***************@yahoo.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>

One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?
Because a php can contain other text such as html... the parser is only
signaled to work on the block that is inside a the php tag...

you ever wonder why <? php ?looks like an html tag? cause it is!
May 28 '07 #2

P: n/a
On May 28, 6:03 pm, Michael <MichaelDMcDonn...@yahoo.comwrote:
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>

One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?

Thanks,
M. McDonnell
It's not supposed to be bracketed by <?phpand <?>, but by <?php and ?
>. That's not necessary, though. You have to put those symbols into
your require-d file if you want it to be understood like php code, but
if you don't, it will be understood as raw output. Just like any other
php file:
<?php
if ( $x ) {
?>
some output here
some output here
some output here
<?php
} else {
?>
some other output here
some other output here
some other output here
<?php

May 28 '07 #3

P: n/a
On May 28, 6:09 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
"Michael" <MichaelDMcDonn...@yahoo.comwrote in message

news:11**********************@z28g2000prd.googlegr oups.com...
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>
One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?
It's not supposed to be bracketed by <?phpand <?>, but by <?php
and ?
>. That's not necessary, though. You have to put those symbols into
your require-d file if you want it to be understood like php code, but
if you don't, it will be understood as raw output. Just like any other
php file:
<?php
if ( $x ) {
?>
some output here
some output here
some output here
<?php
} else {
?>
some other output here
some other output here
some other output here
<?php
}
?>

As for the require-d file, the same counts - if you didn't put <?php
and ?surrounding the contents of the file, they would be understood
as ordinary output, not php code (which, of course, can be exactly
what we wanted).
>
Because a php can contain other text such as html... the parser is only
signaled to work on the block that is inside a the php tag...

you ever wonder why <? php ?looks like an html tag? cause it is!
I wouldn't go that far to say <?php and ?are html tags, although
they do resemble them having lt and gt chars. Actually, the <? and ?>
are part of xml processing instruction declaration syntax, so when we
say "<?php" we say "give the following contents to php", and when we
say "?>", we actually say "those contents end here". So, php code is
actually organized as xml document, and html out of <?php ?>
instructions perfectly fits in that concept, being itself a subset of
xml, although older than xml.

May 28 '07 #4

P: n/a
On May 28, 9:24 am, Darko <darko.maksimo...@gmail.comwrote:
On May 28, 6:09 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:"Michael" <MichaelDMcDonn...@yahoo.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>
One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?

It's not supposed to be bracketed by <?phpand <?>, but by <?php
and ?
. That's not necessary, though. You have to put those symbols into

your require-d file if you want it to be understood like php code, but
if you don't, it will be understood as raw output. Just like any other
php file:
<?php
if ( $x ) {
?>
some output here
some output here
some output here
<?php
} else {
?>
some other output here
some other output here
some other output here
<?php
}
?>

As for the require-d file, the same counts - if you didn't put <?php
and ?surrounding the contents of the file, they would be understood
as ordinary output, not php code (which, of course, can be exactly
what we wanted).
Because a php can contain other text such as html... the parser is only
signaled to work on the block that is inside a the php tag...
you ever wonder why <? php ?looks like an html tag? cause it is!

I wouldn't go that far to say <?php and ?are html tags, although
they do resemble them having lt and gt chars. Actually, the <? and ?>
are part of xml processing instruction declaration syntax, so when we
say "<?php" we say "give the following contents to php", and when we
say "?>", we actually say "those contents end here". So, php code is
actually organized as xml document, and html out of <?php ?>
instructions perfectly fits in that concept, being itself a subset of
xml, although older than xml.
=====================
I guess I'm still missing the point. Once the browser interpreter sees
the <?php, it assumes that all text to follow is php code until the
terminating token ?is seen. Now if the include function is called
within the <?php ... ?>, the interpreter still assumes that any text
it sees is PHP. So again, I don't understand why the include text
needs to bracket its code in <?php ...?>. What am I missing here?
Thanks, MDM.

May 28 '07 #5

P: n/a

"Michael" <Mi***************@yahoo.comwrote in message
news:11*********************@x35g2000prf.googlegro ups.com...
On May 28, 9:24 am, Darko <darko.maksimo...@gmail.comwrote:
>On May 28, 6:09 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:>
"Michael" <MichaelDMcDonn...@yahoo.comwrote in message
>news:11**********************@z28g2000prd.googleg roups.com...
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>
One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?

It's not supposed to be bracketed by <?phpand <?>, but by <?php
and ?
>. That's not necessary, though. You have to put those symbols into

your require-d file if you want it to be understood like php code, but
if you don't, it will be understood as raw output. Just like any other
php file:
<?php
if ( $x ) {
?>
some output here
some output here
some output here
<?php
} else {
?>
some other output here
some other output here
some other output here
<?php
}
?>

As for the require-d file, the same counts - if you didn't put <?php
and ?surrounding the contents of the file, they would be understood
as ordinary output, not php code (which, of course, can be exactly
what we wanted).
Because a php can contain other text such as html... the parser is only
signaled to work on the block that is inside a the php tag...
you ever wonder why <? php ?looks like an html tag? cause it is!

I wouldn't go that far to say <?php and ?are html tags, although
they do resemble them having lt and gt chars. Actually, the <? and ?>
are part of xml processing instruction declaration syntax, so when we
say "<?php" we say "give the following contents to php", and when we
say "?>", we actually say "those contents end here". So, php code is
actually organized as xml document, and html out of <?php ?>
instructions perfectly fits in that concept, being itself a subset of
xml, although older than xml.

=====================
I guess I'm still missing the point. Once the browser interpreter sees
the <?php, it assumes that all text to follow is php code until the
terminating token ?is seen. Now if the include function is called
within the <?php ... ?>, the interpreter still assumes that any text
it sees is PHP. So again, I don't understand why the include text
needs to bracket its code in <?php ...?>. What am I missing here?
Thanks, MDM.
Thats your problem... php has nothing to do with the browser. Its a server
side scripting language and independent of the client.
May 28 '07 #6

P: n/a
On May 28, 10:05 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com>
wrote:
"Michael" <MichaelDMcDonn...@yahoo.comwrote in message

news:11*********************@x35g2000prf.googlegro ups.com...


On May 28, 9:24 am, Darko <darko.maksimo...@gmail.comwrote:
On May 28, 6:09 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:>
"Michael" <MichaelDMcDonn...@yahoo.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>
One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?
It's not supposed to be bracketed by <?phpand <?>, but by <?php
and ?
. That's not necessary, though. You have to put those symbols into
your require-d file if you want it to be understood like php code, but
if you don't, it will be understood as raw output. Just like any other
php file:
<?php
if ( $x ) {
?>
some output here
some output here
some output here
<?php
} else {
?>
some other output here
some other output here
some other output here
<?php
}
?>
As for the require-d file, the same counts - if you didn't put <?php
and ?surrounding the contents of the file, they would be understood
as ordinary output, not php code (which, of course, can be exactly
what we wanted).
Because a php can contain other text such as html... the parser is only
signaled to work on the block that is inside a the php tag...
you ever wonder why <? php ?looks like an html tag? cause it is!
I wouldn't go that far to say <?php and ?are html tags, although
they do resemble them having lt and gt chars. Actually, the <? and ?>
are part of xml processing instruction declaration syntax, so when we
say "<?php" we say "give the following contents to php", and when we
say "?>", we actually say "those contents end here". So, php code is
actually organized as xml document, and html out of <?php ?>
instructions perfectly fits in that concept, being itself a subset of
xml, although older than xml.
=====================
I guess I'm still missing the point. Once the browser interpreter sees
the <?php, it assumes that all text to follow is php code until the
terminating token ?is seen. Now if the include function is called
within the <?php ... ?>, the interpreter still assumes that any text
it sees is PHP. So again, I don't understand why the include text
needs to bracket its code in <?php ...?>. What am I missing here?
Thanks, MDM.

Thats your problem... php has nothing to do with the browser. Its a server
side scripting language and independent of the client.- Hide quoted text -

- Show quoted text -
=============================
Yes, the text "browser interpreter" should replaced by "interpreter".
In any case, it would seem that the question is still valid. Any
comments would be appreciated.
MDM

May 28 '07 #7

P: n/a
On May 28, 12:45 pm, Michael <MichaelDMcDonn...@yahoo.comwrote:
=============================
Yes, the text "browser interpreter" should replaced by "interpreter".
In any case, it would seem that the question is still valid. Any
comments would be appreciated.
MDM
I think I see what you're getting at. If you include a file, you still
need parser instructions for the contents of the included file, since
the include file can have processor directives just the same as the
including script does. To wit:

<code file="index.php">
<?php
include 'html-header.inc.php';
include 'main.inc.php';
include 'html-footer.inc.php';
?>
</code>

<code file="html-header.inc.php">
<html>
<head><title><?php echo COMPANY_NAME; ?></title></head>
<body>
<?php include 'company-header.inc.php'; ?>
</code>

<code file="main.inc.php">
<?php
$inc = '/LIB/section-'.$_GET['section'].'inc.php';
include $inc;
?>
</code>

<code file="html-footer.inc.php">
<?php include 'company-footer.inc.php'; ?>
</body>
</html>
</code>

So as you see, you have to have <?php ?tags on included content, as
that included content may still contain a mix of processor directives
and hard-coded data (like html).

Incidentally, you really only need <?php if a page is all php; the
parser will close the file and auto-insert a ?to finish. However, I
generally this to be bad practice and always insert it anyways.

May 28 '07 #8

P: n/a
Michael wrote:
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>

One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?
Unlike, say, C preprocessor instructions or java's imports, php
include() is executed when your script runs, not when it compiles. When
interpreter encounters an "include", it reads the given file and
evaluates its context as php code. "include" is just a confusing name,
"included" files are actually not "embedded" in main file.
--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
May 28 '07 #9

P: n/a
On May 28, 10:05 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com>
wrote:
"Michael" <MichaelDMcDonn...@yahoo.comwrote in message

news:11*********************@x35g2000prf.googlegro ups.com...


On May 28, 9:24 am, Darko <darko.maksimo...@gmail.comwrote:
On May 28, 6:09 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:>
"Michael" <MichaelDMcDonn...@yahoo.comwrote in message
news:11**********************@z28g2000prd.googlegr oups.com...
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>
One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?
It's not supposed to be bracketed by <?phpand <?>, but by <?php
and ?
. That's not necessary, though. You have to put those symbols into
your require-d file if you want it to be understood like php code, but
if you don't, it will be understood as raw output. Just like any other
php file:
<?php
if ( $x ) {
?>
some output here
some output here
some output here
<?php
} else {
?>
some other output here
some other output here
some other output here
<?php
}
?>
As for the require-d file, the same counts - if you didn't put <?php
and ?surrounding the contents of the file, they would be understood
as ordinary output, not php code (which, of course, can be exactly
what we wanted).
Because a php can contain other text such as html... the parser is only
signaled to work on the block that is inside a the php tag...
you ever wonder why <? php ?looks like an html tag? cause it is!
I wouldn't go that far to say <?php and ?are html tags, although
they do resemble them having lt and gt chars. Actually, the <? and ?>
are part of xml processing instruction declaration syntax, so when we
say "<?php" we say "give the following contents to php", and when we
say "?>", we actually say "those contents end here". So, php code is
actually organized as xml document, and html out of <?php ?>
instructions perfectly fits in that concept, being itself a subset of
xml, although older than xml.
=====================
I guess I'm still missing the point. Once the browser interpreter sees
the <?php, it assumes that all text to follow is php code until the
terminating token ?is seen. Now if the include function is called
within the <?php ... ?>, the interpreter still assumes that any text
it sees is PHP. So again, I don't understand why the include text
needs to bracket its code in <?php ...?>. What am I missing here?
Thanks, MDM.

Thats your problem... php has nothing to do with the browser. Its a server
side scripting language and independent of the client.- Hide quoted text -

- Show quoted text -
=============================
Yes, the text "browser interpreter" should replaced by "interpreter".
In any case, it would seem that the question is still valid. Any
comments would be appreciated.
MDM

May 28 '07 #10

P: n/a
Michael wrote:
On May 28, 10:05 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com>
wrote:
>"Michael" <MichaelDMcDonn...@yahoo.comwrote in message

news:11*********************@x35g2000prf.googlegr oups.com...


>>On May 28, 9:24 am, Darko <darko.maksimo...@gmail.comwrote:
On May 28, 6:09 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:>
"Michael" <MichaelDMcDonn...@yahoo.comwrote in message
news:11**********************@z28g2000prd.goog legroups.com...
>Since the include function is called from within a PHP script, why
>does the included file have to identify itself as a PHP again by
>enclosing its code in <?php... <?>
>One would assume that the PHP interpreter works like any other, that
>is, it first expands all the include files, and then parses the
>resulting text. Can anyone help with an explanation?
It's not supposed to be bracketed by <?phpand <?>, but by <?php
and ?
. That's not necessary, though. You have to put those symbols into
your require-d file if you want it to be understood like php code, but
if you don't, it will be understood as raw output. Just like any other
php file:
<?php
if ( $x ) {
?>
some output here
some output here
some output here
<?php
} else {
?>
some other output here
some other output here
some other output here
<?php
}
?>
As for the require-d file, the same counts - if you didn't put <?php
and ?surrounding the contents of the file, they would be understood
as ordinary output, not php code (which, of course, can be exactly
what we wanted).
Because a php can contain other text such as html... the parser is only
signaled to work on the block that is inside a the php tag...
you ever wonder why <? php ?looks like an html tag? cause it is!
I wouldn't go that far to say <?php and ?are html tags, although
they do resemble them having lt and gt chars. Actually, the <? and ?>
are part of xml processing instruction declaration syntax, so when we
say "<?php" we say "give the following contents to php", and when we
say "?>", we actually say "those contents end here". So, php code is
actually organized as xml document, and html out of <?php ?>
instructions perfectly fits in that concept, being itself a subset of
xml, although older than xml.
=====================
I guess I'm still missing the point. Once the browser interpreter sees
the <?php, it assumes that all text to follow is php code until the
terminating token ?is seen. Now if the include function is called
within the <?php ... ?>, the interpreter still assumes that any text
it sees is PHP. So again, I don't understand why the include text
needs to bracket its code in <?php ...?>. What am I missing here?
Thanks, MDM.
Thats your problem... php has nothing to do with the browser. Its a server
side scripting language and independent of the client.- Hide quoted text -

- Show quoted text -
=============================
Yes, the text "browser interpreter" should replaced by "interpreter".
In any case, it would seem that the question is still valid. Any
comments would be appreciated.
MDM
Michel,

Because the included file may not necessarily contain PHP code. It can
contain html, for instance, with php code intermixed - just like any php
file. Or it may contain no php code at all.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 28 '07 #11

P: n/a
On May 28, 11:46 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
Michael wrote:
On May 28, 10:05 am, "Jon Slaughter" <Jon_Slaugh...@Hotmail.com>
wrote:
"Michael" <MichaelDMcDonn...@yahoo.comwrote in message
>news:11*********************@x35g2000prf.googlegr oups.com...
>On May 28, 9:24 am, Darko <darko.maksimo...@gmail.comwrote:
On May 28, 6:09 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:>
"Michael" <MichaelDMcDonn...@yahoo.comwrote in message
news:11**********************@z28g2000prd.goog legroups.com...
Since the include function is called from within a PHP script, why
does the included file have to identify itself as a PHP again by
enclosing its code in <?php... <?>
One would assume that the PHP interpreter works like any other, that
is, it first expands all the include files, and then parses the
resulting text. Can anyone help with an explanation?
It's not supposed to be bracketed by <?phpand <?>, but by <?php
and ?
. That's not necessary, though. You have to put those symbols into
your require-d file if you want it to be understood like php code, but
if you don't, it will be understood as raw output. Just like any other
php file:
<?php
if ( $x ) {
?>
some output here
some output here
some output here
<?php
} else {
?>
some other output here
some other output here
some other output here
<?php
}
?>
As for the require-d file, the same counts - if you didn't put <?php
and ?surrounding the contents of the file, they would be understood
as ordinary output, not php code (which, of course, can be exactly
what we wanted).
Because a php can contain other text such as html... the parser is only
signaled to work on the block that is inside a the php tag...
you ever wonder why <? php ?looks like an html tag? cause it is!
I wouldn't go that far to say <?php and ?are html tags, although
they do resemble them having lt and gt chars. Actually, the <? and ?>
are part of xml processing instruction declaration syntax, so when we
say "<?php" we say "give the following contents to php", and when we
say "?>", we actually say "those contents end here". So, php code is
actually organized as xml document, and html out of <?php ?>
instructions perfectly fits in that concept, being itself a subset of
xml, although older than xml.
=====================
I guess I'm still missing the point. Once the browser interpreter sees
the <?php, it assumes that all text to follow is php code until the
terminating token ?is seen. Now if the include function is called
within the <?php ... ?>, the interpreter still assumes that any text
it sees is PHP. So again, I don't understand why the include text
needs to bracket its code in <?php ...?>. What am I missing here?
Thanks, MDM.
Thats your problem... php has nothing to do with the browser. Its a server
side scripting language and independent of the client.- Hide quoted text -
- Show quoted text -
=============================
Yes, the text "browser interpreter" should replaced by "interpreter".
In any case, it would seem that the question is still valid. Any
comments would be appreciated.
MDM

Michel,

Because the included file may not necessarily contain PHP code. It can
contain html, for instance, with php code intermixed - just like any php
file. Or it may contain no php code at all.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================- Hide quoted text -

- Show quoted text -
======================
Thank you all for your very helpful answers to my question.

May 28 '07 #12

P: n/a
Message-ID: <11*********************@x35g2000prf.googlegroups. comfrom
Michael contained the following:
>I guess I'm still missing the point. Once the browser interpreter sees
the <?php, it assumes that all text to follow is php code until the
terminating token ?is seen. Now if the include function is called
within the <?php ... ?>, the interpreter still assumes that any text
it sees is PHP. So again, I don't understand why the include text
needs to bracket its code in <?php ...?>. What am I missing here?
Ummm...reading the manual?

http://uk.php.net/include/

"When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the end.
For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags."

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
May 29 '07 #13

P: n/a
Ummm...reading the manual?
>
http://uk.php.net/include/

"When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the end.
For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags."
Interesting topic this has been, and at last there is the correct
answer. I can easily imagine a situation where and included file,
whether being html, plain text or php, would be sometimes run
separately, sometimes as included file. If including would require
the ?-tags or <?php -tags, the whole include -functionality would
become unusable. This kind of behavior could of course be done with a
parameter that would determine whether those tags would be needed or
not, but that's another story then.

On the other issue about <?php -tags. In no way <?php is an html -tag.
It is not one, period. <% is not html tag, <cfoutput is not an html -
tag, <?= is not an html tag. Neither is any other markup that is used
in some own server side software to identify interpreted parts of the
code. Html -tags are listed in: http://www.w3.org/TR/html401/index/elements.html
..

--
Jussi T
http://view.fi
http://naamio.net
http://hoffburger.com

May 29 '07 #14

P: n/a
Message-ID: <11**********************@k79g2000hse.googlegroups .comfrom
ju*****@gmail.com contained the following:
>On the other issue about <?php -tags. In no way <?php is an html -tag.
It is not one, period.
Indeed. In normal circumstances the browser never sees it.
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
May 29 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.