Jump to content

[SOLVED] <?php ?> Tags in Included Files


Mzor

Recommended Posts

So, this is something I've been wondering about, and worrying about. Most of my PHP documents will have something like:

 

<?php

include ('./include/header.php');

//Content goes here

include ('./include/footer.php');

?>

 

Not something we haven't all seen before, but often my 'header.php' file contains PHP tags itself. Say the header consisted of this:

 

//Html stuff and CSS rules here

<?php

if(isset($_SESSION['username'])){
   echo 'Hello, ' . $_SESSION['username'];
}

?>

 

Then, logically, the file should end up like this, when the header file was included:

 

<?php

//Html stuff and CSS rules here

<?php

if(isset($_SESSION['username'])){
   echo 'Hello, ' . $_SESSION['username'];
}

?>

//Content goes here

include ('./include/footer.php');

?>

 

Not only, then, would the HTML and CSS be inside the PHP, tags, there would be PHP tags inside the PHP tags, meaning that the 'include ('./include/footer.php');' should be ignored... yet it always works for me...

 

Can someone explain so I don't end up making mistakes with this later which I'll nver be able to figure out?

Link to comment
https://forums.phpfreaks.com/topic/150230-solved-tags-in-included-files/
Share on other sites

PHP's include command isn't a preprocessor directive (in the C/C++ sense), it's more of a language construct (well, actually it is a construct).

 

 

When ever you include a file, the content isn't just smacked into the file and parsed.  The file is opened in "text" mode (to simplify things), therefore, to switch back to PHP, tags are needed again in the file.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.