| | |
Help using "include" inside of "echo"
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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 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.
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';
?>
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';
?>
Gabriel Villa
software development/ IT support consultant
software development/ IT support consultant
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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.
Produces:
I want some $pie.
I want some fries.
PHP Syntax (Toggle Plain Text)
$pie="fries"; echo 'I want some $pie. \n'; 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.
www.uncreativelabs.net
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
•
•
•
•
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
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
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.
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]
[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]
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
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
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.
![]() |
Similar Threads
- "Craps", Game Help (C++)
- google "keyword" question (Search Engine Optimization)
- "res://" message in address bar, no pages can be displayed (Viruses, Spyware and other Nasties)
Other Threads in the PHP Forum
- Previous Thread: Continue?
- Next Thread: hello i need help please
| Thread Tools | Search this Thread |
apache api array auto beginner binary broken cache cakephp checkbox class cms code codingproblem cron curl customizableitems database date display dynamic echo email error file files filter folder form format forms forum function functions gc_maxlifetime global google headmethod host href htaccess html image include insert ip javascript joomla limit link login mail malfunctioning memmory memory menu mlm multiple mysql nodes oop parameter parsing paypal pdf php phpmysql popup query radio random recursion recursiveloop remote script search select server sessions sms snippet source space sql static survey syntax system table trouble tutorial up-to-date update upload url validator variable video web youtube






