944,188 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 15262
  • PHP RSS
Sep 20th, 2006
0

Help using "include" inside of "echo"

Expand Post »
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.
Last edited by okparrothead; Sep 20th, 2006 at 5:16 pm.
Similar Threads
Reputation Points: 14
Solved Threads: 0
Light Poster
okparrothead is offline Offline
31 posts
since Sep 2006
Sep 20th, 2006
0

Re: Help using "include" inside of "echo"

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';
?>
Reputation Points: 8
Solved Threads: 6
Posting Whiz in Training
extofer is offline Offline
239 posts
since Aug 2005
Sep 20th, 2006
0

Re: Help using "include" inside of "echo"

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.

PHP Syntax (Toggle Plain Text)
  1. $pie="fries";
  2. echo 'I want some $pie. \n';
  3.  
  4. echo "I want some $pie.";

Produces:
I want some $pie.
I want some fries.
Last edited by Puckdropper; Sep 20th, 2006 at 6:18 pm.
Reputation Points: 23
Solved Threads: 23
Posting Pro in Training
Puckdropper is offline Offline
494 posts
since Jul 2004
Sep 20th, 2006
0

Re: Help using "include" inside of "echo"

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Sep 22nd, 2006
0

Re: Help using "include" inside of "echo"

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
Last edited by okparrothead; Sep 22nd, 2006 at 5:48 pm.
Reputation Points: 14
Solved Threads: 0
Light Poster
okparrothead is offline Offline
31 posts
since Sep 2006
Sep 23rd, 2006
0

Re: Help using "include" inside of "echo"

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

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

[/php]

NOT

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

[/php]
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Oct 6th, 2006
0

Re: Help using "include" inside of "echo"

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:

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

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:

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

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
Last edited by tgreer; Oct 6th, 2006 at 7:32 pm. Reason: Fixed code tags: hint, need a "/" in the closing tag.
Reputation Points: 14
Solved Threads: 0
Light Poster
okparrothead is offline Offline
31 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Continue?
Next Thread in PHP Forum Timeline: hello i need help please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC