0

I'd like to include the results of a given script (let's say a.php) into another script (let's say b.php).

Although there is the PHP include statement and their other counterparts (include_once, require and require_once), it seems all of them include the file contents instead of its processing results as I would expect.

So how can I get a.php processed first and then include its results (not the file contents itself) into b.php script ?

On the other hand, if my included file (a.php) has a return statement, is the entire file expected to be processed first or its contents are also included into b.php as is ?

Thanks in advance for your replies.

4
  • just include a.php into b.php on top or in starting of script Commented Jun 17, 2012 at 15:52
  • 1
    Can you please give an example to clarify, why include/require don't suit your needs? Commented Jun 17, 2012 at 15:52
  • 1
    Yes it does seem that include/require is what you want, although it may be that you also need output buffering, e.g. ob_start(); include 'a.php'; $resultOfA = ob_get_clean(); Commented Jun 17, 2012 at 15:57
  • I believe that both Themroc and georgefox provided very good answers. As my files are all inside my server (not external), Themroc's answer applies better for what I need so I will mark his answer as a solution to this question. Tim, I followed your suggestion and researched a little more about this approach. You are totaly right. Thank you all guys. Commented Jun 18, 2012 at 14:38

4 Answers 4

4

a.php:

<?php
echo "world";

b.php:

<?php
ob_start();
include("a.php");
$a= ob_get_clean();
echo "Hello, $a!";  

perhaps?

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

Comments

1

Sounds like you want to use the backtick operator followed by eval. Something like this:

$code = `/path/to/myscript.php`;
eval($code);

If myscript.php isn't executable you'll have to prefix it with php, as in:

$code = `php /path/to/myscript.php`;

Note that you need to completely trust myscript.php, otherwise this is an security problem.

This probably isn't a very good design, you might want to think hard about whether or not there's a better way to do what you want.

Comments

0

If a.php is accessible via the web, you could use file_get_contents with an http:// (or https://) URL. This will access your script like an ordinary web page, having your web server process it through PHP and return the results.

a.php:

<?php

echo 2+2;

b.php:

<?php

echo file_get_contents('http://example.com/a.php');

Output:

4

You can also use output buffering, as mentioned in DaveRandom's comment above. That said, this is generally not exemplary application design. It's preferred to have code that will return the results you need rather than echo them. So maybe something like this:

a.php:

<?php

function calculateA()
{
    return 2+2;
}

b.php:

<?php

include('a.php');
echo calculateA();

Output:

4

Edit: Several alternatives are nicely detailed on the include page in the PHP documentation.

Comments

-1

Summary:

If php file belongs to your server:

ob_start();
$result = eval(file_get_contents('/path/to/your/script.php'));
ob_end_clean();

If not, but it's accessible via HTTP:

$result = file_get_contents('http://path.to/your/script.php');

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.