Forum Moderators: coopster

Message Too Old, No Replies

can't get PHP include to work

this should be easy, I think....

         

Craig_F

6:21 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



I'm testing a PHP script and I want to include another PHP file into the template that the script ultimately outputs. I do not know PHP, but I have done includes before without issue, but not with this script. I think it is due to the way the template is set up, but I don't know what I need to change to get it to work.

The template looks like this:

<?php
$templ = '

Regular HTML Here

<?php include 'TestInclude.php';?>

More Regular HTML Here

';
?>

No matter what I try I get a T_string error each time I try to put that include in there like that. Is there some other way I should be doing this?

Thanks!

grandpa

6:31 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi Craig

The problem I see is that you've the include, and all the html, defined as the variable $templ.

I'm not sure what $templ should be, but the include should be placed outside the tic marks, thusly:

<?php
$templ = 'Regular HTML Here More Regular HTML Here ';

include 'TestInclude.php';

?>

Also, you have enclosed the include within the php tags, which is placed inside php tags, and that just won't do.
Note I removed the php tags from the include. Hopefully that will get you going.

<edit>
Looking at your example, this is probably closer to what you are trying to accomplish:

<?php
$templ = 'Regular HTML Here';
$templ .= include ('TestInclude.php');
$templ .= 'More Regular HTML Here ';
?>

I can't say for sure that the include will work like this, if not, bring it back and we'll have another look.
</edit>

Craig_F

7:11 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



thanks grandpa, I'll post back once I get a chance to try otu your suggestions.

grandpa

10:20 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've been toying with this for a bit, and the sample I provided does not work.

<?php
$templ = 'Regular HTML Here ';
$templ .= include ('TestInclude.php');
$templ .= ' More Regular HTML Here';
echo "$templ";
?>

Displays:
Regular HTML Here 1 More Regular HTML Here
if the include file is found, and
Regular HTML Here More Regular HTML Here
if the include file is not found.

The content of the include displays above the echoed output.
(I placed some content in the include so I could observe the results.) Basically, only the status, either 1 or NULL is being returned to the string.
We've almost exceeded my limited knowledge... anyone else?

jinkas

5:23 am on Feb 18, 2005 (gmt 0)



So include will just dump the contents of whatever you are including into the script where you have included it. You can't use include to assign something to a variable, because include (as you have discovered) only returns 0 or 1, depending on whether or not the file was successfully included (ie. dumped) into the file where you used the include statement. If you want to assign a file to a variable, there are two (common) methods:

$filearray=file('/path/to/file.ext'); will put the contents into an array, one line per element

$filestring=file_get_contents('/path/to/file.ext'); will put the contents into a string

You can find out more about each of these functions (as well as other methods, such as using fopen() and fgets()) at the PHP manual at [php.net...]