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:
[PHP]return "some string to return";[/PHP]
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]
<?php
$string = include('child.php');
var_dump($string);
?>
[/PHP]
Child File:
[PHP]<?php return 'string from child'; ?>[/PHP]
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]<?php echo 'string from child'; ?>[/PHP]
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:
[PHP]echo 'foo';[/PHP]
sends 'foo' to php output.
[PHP]return 'foo'; [/PHP]
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]
<?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
?>
[/PHP]
Child File:
[PHP]<?php echo 'string from child'; ?>[/PHP]
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:
[PHP]
$string = file_get_contents('child.php');
[/PHP]
your can also use fopen() and fread() to achieve the same.