Jump to content

Archived

This topic is now archived and is closed to further replies.

ajsuk

[Solved]How to echo PHP in to an include?

Recommended Posts

So I'd like to be able to echo php in to an include, but php really doesn't seem to like me trying to include php in an echo.

Heres an example of what I've tried, hopefully you can grasp the idea of what I'm trying to do. :)

Trying to echo in bit of PHP:
[code]<?php
$text = '<? echo $status[2] ?>';
include("/blahblah/myinclude.php")
?>[/code]

Then output in to the included file:
[code]<?php
echo "$text"; // Text Inc
?>[/code]

It doesn't like being formated like that, could somebody tell me what I'm doing wrong and how to fix it?

Thanks in advance!  8)

Share this post


Link to post
Share on other sites
Depends on what you do in your include file before you echo the variable.

Ronald  8)

Share this post


Link to post
Share on other sites
I think it's this bit thats screwing it up:
[code]$text = '<? echo $status[2] ?>';[/code]

It'll include whats in there no probs until I put the php tags around it... And putting them in at the other end (in the included file) doesn't work either.

Share this post


Link to post
Share on other sites
Why are you putting tags around this?

[code=php:0]
<? echo $status[2] ?>
[/code]

You are already in PHP mode with this code block:

[code=php:0]
<?php
$text = '<? echo $status[2] ?>';
include("/blahblah/myinclude.php")
?>
[/code]

Try this:

[code=php:0]
<?php
$text = $status[2];
include("/blahblah/myinclude.php")
?>
[/code]

Share this post


Link to post
Share on other sites
because it has to act as PHP when its transfered in to the include page.

Share this post


Link to post
Share on other sites
When you include a file into a script, it is treated as inline code by the PHP processor. It is no different than if you typed in yourself.

How do you think included files work?

Ken

Share this post


Link to post
Share on other sites
Ajsuk ... we may have a problem here. As you have coded yourself before the $text=... statement, you are already in PHP when you do the assignment to $text.

Whatever you assign, in this PHP part, to $text is in variable $text. When you assign 'Balblablax' to it, it will still contain 'Balblablax' when you echo it in your myinclude.php file.


Share this post


Link to post
Share on other sites
if the code works good luck.

dam beat to it,
[code]

<?php

$stat=array("what_ever","what_ever","my name is redarrow");

foreach($stat AS $status ){

$text = $status[2];

include("/blahblah/myinclude.php");

}

?>
[/code]

Share this post


Link to post
Share on other sites
Yeah  :-\

So is there any way to change the "Balblablax" to "<? echo $status[2] ?>" without it getting annoyed?

the "<? echo $status[2] ?>" bit is supposed to trigger another script earlier in the include file to produce a server status (from server #3) and I [u]only[/u] want it to display that 3rd server result in this case. So this is why I'm trying to tell it to echo that status in this example.

If theres any other way to do it, I'm all ears. Sorry for the confusion, I'm not up on all this.  :(

Share this post


Link to post
Share on other sites
But your include file is already in PHP mode:

[b]/blahblah/myinclude.php[/b]
[code]
<?php
echo "$text"; // Text Inc
?>
[/code]

Which translates to bring read as:
[code]
echo '<? echo $status[2] ?>';
[/code]

Share this post


Link to post
Share on other sites
Did you try this?

[code=php:0]
<?php
$text = $status[2];
include("/blahblah/myinclude.php")
?>
[/code]

If not, try it out and let us know if it works.

Share this post


Link to post
Share on other sites
ah, yes, I've got it!! *smacks head on table* Must be one of those days, thanks alot all!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.