29

I want to call require_once("test.php") but not display result and save it into variable like this:

$test = require_once('test.php');

//some operations like $test = preg_replace(…);

echo $test;

Solution:

test.php

<?php
$var = '/img/hello.jpg';

$res = <<<test

<style type="text/css">
body{background:url($var)#fff !important;}
</style>

test;

return $res;
?>

main.php

<?php
$test = require_once('test.php');

echo $test;
?>
automatix
  • 14,018
  • 26
  • 105
  • 230
swamprunner7
  • 1,321
  • 6
  • 16
  • 25

3 Answers3

40

Is it possible?

Yes, but you need to do an explicit return in the required file:

//test.php

<? $result = "Hello, world!"; 
  return $result;
?>

//index.php

$test = require_once('test.php');  // Will contain "Hello, world!"

This is rarely useful - check Konrad's output buffer based answer, or adam's file_get_contents one - they are probably better suited to what you want.

sanchez
  • 4,519
  • 3
  • 23
  • 53
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I want to execute my php file and return html, js and etc but prepeared by php. Can i do it without $a = 'my html and etc'; return $a; ? – swamprunner7 May 13 '10 at 21:21
  • @swamprunner you could use HEREDOC syntax, but it's not really what you want I think. Better go with `file_get_contents()` or Konrad's solution. – Pekka May 13 '10 at 21:22
  • Most PHP programmers don't even know this is possible, thank goodness :) – webbiedave May 13 '10 at 21:34
  • @webbiedave why exactly "thank goodness"? What is wrong with it to for example, return a callback? – Timo Huovinen Dec 01 '13 at 08:34
  • @TimoHuovinen I was being a tad hyperbolic. I've seen good uses of this feature (Kohana, for example, uses them as a way to read in specially formatted config files). My point was that if most programmers were aware of this we'd see much *misuse* of it (like using it for templates). – webbiedave Dec 01 '13 at 13:54
  • @webbiedave oh I see, and I agree that it's not a good choice for templates :) – Timo Huovinen Dec 01 '13 at 15:44
36

“The result” presumably is a string output?

In that case you can use ob_start to buffer said output:

ob_start();
require_once('test.php');
$test = ob_get_contents();

EDIT From the edited question it looks rather like you want to have a function inside the included file. In any case, this would probably be the (much!) cleaner solution:

<?php // test.php:

function some_function() {
    // Do something.
    return 'some result';
}

?>

<?php // Main file:

require_once('test.php');

$result = test_function(); // Calls the function defined in test.php.
…
?>
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 4
    just for the sake of completeness: if you use 'ob_start()', don't forget to 'ob_end_clean()' after assigning the output to the variable. – sasha Nov 08 '14 at 12:52
4

file_get_contents will get the content of the file. If it's on the same server and referenced by path (rather than url), this will get the content of test.php. If it's remote or referenced by url, it will get the output of the script.

Adam Hopkinson
  • 28,281
  • 7
  • 65
  • 99