Hello all,

I have a newbie question.

I need an alternative to this:

$variable = 'Sometext with some other text <?php include 'somescript.php'?> some more text';

where $variable will be echoed into a webpage as part of the page content.

I can echo text into the page all day, but not an include.

I'm guessing that the echo command doesn't process the variable, but I don't know what command to use to process the include on the way to, or after its in, the page.

Would it be fwrite? If so, how can I get it to write to the file the $variable is in?

Thanks for all your help.

I'm assuming you want to concantinate the include into the text.

Try setting the include text as a varialbe like then concantinate it to your original variable.

<?
$varInc = include 'somescript.php'?> some more text';

$variable = 'Sometext with some other text' + $varInc + 'some more text';
?>

Single quotes are literal indicators. They say "print exactly this until you get to the next single quote. To expand something out, use double quotes.

$pie="fries";
echo 'I want some $pie. \n';

echo "I want some $pie.";

Produces:
I want some $pie.
I want some fries.

Hello all,

I have a newbie question.

I need an alternative to this:

$variable = 'Sometext with some other text <?php include 'somescript.php'?> some more text';

where $variable will be echoed into a webpage as part of the page content.

I can echo text into the page all day, but not an include.

I'm guessing that the echo command doesn't process the variable, but I don't know what command to use to process the include on the way to, or after its in, the page.

Would it be fwrite? If so, how can I get it to write to the file the $variable is in?

Thanks for all your help.

Hi okparrothead,

From your code, im assuming that you want to save the output from a file to a variable.

The include() function returns a true, or false boolean value depending on whether the file could be included or not.
In other words, include only runs the code in the included file.

You could go around this by placing a return statement in your included file like:

return "some string to return";

But this is not an adequate way of solving your problem, since you may want to do other things with your included file, not just use it to return strings. Not to mention, you may want to include other files, not just php files.

Example:

Parent File:

<?php 
$string = include('child.php'); 
var_dump($string);
?>

Child File:

<?php return 'string from child'; ?>

Result:
When running your parent file, the variable $string will actually hold 'string from child'. This is because of the use of the return statement.

However if you have the included file contain:

Child File:

<?php echo 'string from child'; ?>

Result:
When running your parent file, the variable $string hold a boolean TRUE. This is because the echo function sends the string to php output, not back to your $string variable.
Whats sent back to your $string is the return from the include() function, which is TRUE, meaning the included file exists and was included successfully.

What to note here is the difference between the returned value from a function, and the returned output. The output is sent directly to PHP ouput. The return, is returned to the calling function.

Example:

echo 'foo';

sends 'foo' to php output.

return 'foo';

will return 'foo' to the calling function.

Alternatives:

If you want to capture the php output, you can use the output buffering funcitons in php4+
see: http://us2.php.net/outcontrol

Example:

Parent File:

<?php 
ob_start(); // start buffering php output
include('child.php'); // output from child.php is buffered
$string = ob_get_contents(); // ob_get_content contains the buffered php output
ob_end_clean(); // remove the buffered php output
?>

Child File:

<?php echo 'string from child'; ?>

Result:
When running your parent file, the variable $string will hold 'string from child' since we captured the ouput of the echo statement, instead of sending the output before sending it over HTTP to the client (browser).

Another alternative is to create a connection the the file we want text from, and read its contents.

Example:

$string = file_get_contents('child.php');

your can also use fopen() and fread() to achieve the same.

Thanks for all the help!!

I guess I wasn't too clear with my first explanation, but you probably still answered my question.

I have a php page with $variables for all the content on the page except the menu, header and footer, they're in seperate files.

At the bottom of the page, I include a template that contains echo commands for the strings and include commands for the three other files. It puts the content where I want it in the page. Works great.

Sometimes, however, I want to include a file in the middle of the content, a photo gallery for example, or a googlemap script. Using echo won't let the script run.

I'll try the techniques you taught me and post the answer here when I find out which works.

Thanks again,
You're the best!

Peace

If you have echo statements in the included file then just use something like:

echo 'some text';
include('child.php'); // contains echos
echo 'even more text';

NOT

echo 'some text'.(include('child.php')).'even more text';

Hello All,

Thanks for all of your help.

I couldn't get any of your suggestions to work. Just zoomed over my head I suppose. As Homer would say -- Doh!

Anyway, I did find a solution. :p

In my page template (page.php), along with other divs, I have code that looks like this that make up my web pages:

<div id="content">
        <?php       
            echo $content;          
       ?>
</div>

On another page (about.php for example) I would define a set of variables, one of them being $content, that contained html code to be placed in the assorted divs on the page. At the end of that page, I would "include" page.php.

The $strings would echo in and all would be well except I couldn't put an include in a string and have it process.

So my solution was this:

<div id="content">
        <?php
            if (file_exists($contentfile)) {
            include ($contentfile);
            }
            else {
            echo $content;
            };
       ?>
</div>

I now define both $content and $contentfile in about.php.
If there needs to be an include, I name $contentfile as the path to that file. If I don't need an include, I use $content to put html code into that div.

I know, I know, I'm reinventing the wheel. But hey, I'll get there someday.

Thanks again for all of your help.

It was through trying to get your suggestions to work that I came up with this solution. I wouldn't have gotten there without your help.

Peace,

OKParrothead

This article has been dead for over six months. Start a new discussion instead.