Insert code from a file and parse it?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wrybread
Forum Newbie
Posts: 3
Joined: Thu Jul 24, 2008 4:41 pm

Insert code from a file and parse it?

Post by wrybread »

I'm trying to insert a block of code onto every page in a site, and I'd like the code to be parsed. In other words, I'm using the same chunk of PHP code of every page.

Here's how I'm currently inserting the code:

$header = file_get_contents("/home/path/to/code.php");
echo $header;

This successfully inserts the file as text, but it doesn't process the PHP.

Is there a way to insert a block of PHP code from a file onto every page?

Thanks for any help.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Insert code from a file and parse it?

Post by Eran »

wrybread
Forum Newbie
Posts: 3
Joined: Thu Jul 24, 2008 4:41 pm

Re: Insert code from a file and parse it?

Post by wrybread »

When I do that it still doesn't parse the PHP. I'm doing it like this:

Suppose my file is called test.php and contains:

print "This is a test";

I'm including it like this:

include ("/home/path/to/test.php");

This is inserting the text:

print "This is a test";

Instead of just:

"this is a test".

Am I missing something?
wrybread
Forum Newbie
Posts: 3
Joined: Thu Jul 24, 2008 4:41 pm

Re: Insert code from a file and parse it?

Post by wrybread »

Oops, got it.

In the included file, you need the php tags.

For example:

<?php

print "test";

?>
Post Reply