Mzor Posted March 19, 2009 Share Posted March 19, 2009 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 More sharing options...
corbin Posted March 19, 2009 Share Posted March 19, 2009 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. Link to comment https://forums.phpfreaks.com/topic/150230-solved-tags-in-included-files/#findComment-788961 Share on other sites More sharing options...
pcw Posted March 19, 2009 Share Posted March 19, 2009 Using the PHP tags <?php and ?> will not have any effect when including a file or files that also contain these tags. Link to comment https://forums.phpfreaks.com/topic/150230-solved-tags-in-included-files/#findComment-788962 Share on other sites More sharing options...
jlhaslip Posted March 19, 2009 Share Posted March 19, 2009 an "included" file will be handled as text/html until it sees those php tokens. Link to comment https://forums.phpfreaks.com/topic/150230-solved-tags-in-included-files/#findComment-788980 Share on other sites More sharing options...
Mzor Posted March 19, 2009 Author Share Posted March 19, 2009 Okay... thanks, that makes more sense, lol. Link to comment https://forums.phpfreaks.com/topic/150230-solved-tags-in-included-files/#findComment-788988 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.