bnrup
bnrup

Reputation: 111

How can I echo php with php?

The website that I'm developing uses a "header" and "footer" template. These templates are included on each page of the site. The header template opens the HTML and contains the complete head tag, and then opens the body tag. The footer template closes the body tag and then closes the HTML. The basic structure is as follows:

<?php
//Set the current page name so it can be highlighted in the menu bar
$page="Home";
//For page-specific HEAD includes
$headincludes=array("<some type of code>","<some other type of code>");
include("/assets/template/header.php");
?>
<!--START PAGE CONTENT-->
<!--END PAGE CONTENT-->
<?php
include("/assets/template/footer.php");
?>

The header template dumps the "headincludes" array into the head section of the page as so:

foreach ($headincludes as $toecho)
echo $toecho;

This method works fine for things such as javascript, but when I try to add a PHP statement, it ends up as unparsed PHP in the final page served to the browser. Is there some special method to echoing PHP with PHP?

Upvotes: 0

Views: 377

Answers (8)

Alexander
Alexander

Reputation: 494

If you try to output php file, sure you do that easily but your browser won't able to run.

So you need to:

  • Eval the php file and output the result

or

  • Create somehow html file that you'll output instead of php file.

include_once() or include() are your friends anyway, it'll (just like name reads) include the file and the file is "executed" in that very place. "once" version won't include the same file twice if it was already included before (useful if you do several includes, which in turn do another inclusions).

Upvotes: 0

Dan Lugg
Dan Lugg

Reputation: 20592

Yes, eval() (read evil()) however you're best to reconsider your approach. Perhaps, if you're looking to include a considerable amount of PHP code, pass an array of files that could further be included:

// at the beginning of the file
$includes = array('db.php', 'config.php');


// in your header, or footer (whichever is applicable)
foreach($includes as $file){
    if(file_exists($file)){
        include $file; // or include_once
    }
}

Then, using the dynamic includes, you can manage and load additional code as necessary.

Upvotes: 2

grc
grc

Reputation: 23555

Try this:

echo eval('?>html or javascript<?');
echo eval('php');

Upvotes: 0

yossi
yossi

Reputation: 13315

did you try string concatenate. like this

$headincludes=array("<your html code>".$variable."<more html>","<more>")

Upvotes: 0

xkeshav
xkeshav

Reputation: 54022

whats into $headincludes

note: use include_once

Upvotes: 0

deceze
deceze

Reputation: 522091

Execute the PHP code in your main files and dump the result into $headincludes. There's no need to execute the code in the header, the result won't change.

Or would it? If you're actually trying to inject arbitrary code into other code, you're doing it wrong.

Upvotes: 0

j_freyre
j_freyre

Reputation: 4738

What are you adding to your array?

If you are setting you php like that :

$headincludes=array("$var1","$var2");

it will not work. You have to do like that:

$headincludes=array($var1,$var2);

Upvotes: 0

alex
alex

Reputation: 490253

The way to execute PHP from a string is with eval(), however, it is rarely the right tool to use.

Can you place the PHP you need above $headincludes, and then add its output to $headincludes?

<?php
//Set the current page name so it can be highlighted in the menu bar
$page="Home";

include 'generators.php';

$links = generateLinks('some_arg');

//For page-specific HEAD includes
$headincludes=array("<some type of code>","<some other type of code>", $links);
include("/assets/template/header.php");
...

Upvotes: 0

Related Questions