0

Is it possible to execute PHP code into variable? For example I have index.php and page.php files in the same directory. If you execute page.php you will have a part of HTML page. I want to put this as a string into a variable in index.php. Any suggestions?

6
  • Take a look at es2.php.net/manual/en/function.file-get-contents.php Commented Dec 5, 2012 at 10:18
  • Do you want the result of the page.php to be part of the result of index.php? Commented Dec 5, 2012 at 10:19
  • 1
    @Lobo the OP wants the evaluated result, not the raw content I think... Commented Dec 5, 2012 at 10:20
  • 1
    yes, file_get_contents does not execute PHP code Commented Dec 5, 2012 at 10:23
  • You should use output buffering and include, this way the include will execute the code while the buffer collects the output into the memory. see my comment Commented Dec 5, 2012 at 10:26

6 Answers 6

5

You can use the output buffer:

<?php
ob_start();
include('page.php');
$page = ob_get_clean();
?>

Mind that page.php is still executed in the same context (global variables, functions etc. continue to exist)

In general doing something like that sounds like broken design and page.php should be rewritten properly.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, but this brings the variables from the other script into the scope, which is unnecessary.
2
include('page.php');

This will include everything from page.php into index.php

2 Comments

Nope, that runs the script, but does not get the output.
I need to put as a string into a variable, not just include
0

If you want to execute some of you code from page.php, you can include the file and make a function in page.php like this:

<?php include('page.php');
   $test = function MyHtmlCode();
   echo $text;
?>

1 Comment

That runs the script, but does not get the output.
0

You could use cURL to retrieve the output of the script and save it to a variable. Here's what you would do in index.php:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘page.php’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$contents = curl_exec ($ch);

Please note that it is unnecessary to include the other script (and the associated variables) into your page when all you want is the response. Including the script means you have to avoid conflicts between variable names, function names etc.

Comments

-1

You didn't geve an example code so I'll assume that the page actually prints (using echo, print and etc) and then you can use php output buffering.

<?php


ob_start();

require_once('index.php');

$out = ob_get_contents();

ob_end_clean();

?>

(edit)

This is a solution but its defiantly not the best practice... that would be to but the move the code that prints the HTML into functions that generate the HTML and return it, then move those functions into a separate file that would be included in both index.php and page.php and then simply call the functions. Including a file that should print some output, and warping it with output buffer makes the code unreadable and messy.

Comments

-2

You should try this:

ob_start()
include('page.php');
$variable = ob_get_clean();

This starts an output buffer, includes the page.php file and writes everything that was written to the output buffer to $variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.