0

I want to include a file entirely into a variable. So that I can call this var multiple times and keep the code as clean as possible. But when I echo the var it only returns a 1 and when I use the include on itself it output the entire file.

I want to output the included file and run all php code inside it.

So what am I doing wrong here.

default.php

$jpath_eyecatcher = (JURI::base(). "modules/mod_eyecatcher/tmpl/content/eyecatcher.php");
$jpath_eyecatcher_path = parse_url($jpath_eyecatcher, PHP_URL_PATH);
ob_start();
$eyecatcher = include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
ob_end_clean();


echo $eyecatcher . '<br>';

include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);

echo output is

1

include output is

eyecatchertype = 2 
fontawesome
envelope-o
insert_emoticon
custom-icon-class
128
images/clientimages/research (1).jpg
top
test

Thanks for the help!

purple11111
  • 709
  • 7
  • 20
  • 1
    BTW: 1 as output in this case means that include was successfull. – B001ᛦ Jul 07 '16 at 15:25
  • 1
    I didn't know that thanks man. – purple11111 Jul 07 '16 at 15:25
  • Do you want to get the result of script to variable? – splash58 Jul 07 '16 at 15:27
  • Possible duplicate of [require/include into variable](http://stackoverflow.com/questions/5948395/require-include-into-variable) – Don't Panic Jul 07 '16 at 15:34
  • @Don'tPanic I disagree because I have read that post and couldn't get it to work for the simple reason the example code that Jan Willem provided was not present and in my opinion not properly explained – purple11111 Jul 07 '16 at 15:37
  • @splash58 I want to output the full php code and run it when it gets called in the echo. – purple11111 Jul 07 '16 at 15:37
  • Agree to disagree. When I look at that question, I see multiple answers to that contain examples of using output buffering to do this. But you're certainly entitled to your opinion as well. – Don't Panic Jul 07 '16 at 15:53

4 Answers4

7

Use file_get_contents instead of include()

include() executes the php code given in the file, whereas file_get_contents() gives you the file content.

ByteHamster
  • 4,884
  • 9
  • 38
  • 53
  • Thank you for that information it made me understand now why I can't use the file_get_contents. So maybe my question was a bit unclear sorry about that. – purple11111 Jul 07 '16 at 15:34
7

include is not a function, and normally only returns the status of the include operation:

docs:

Handling Returns: include returns FALSE on failure and raises a warning. Successful includes, unless overridden by the included file, return 1. It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files.

e.g.

x.php:

<?php
return 42;

y.php

<?php
$y = 'foo';

z.php

<?php
$z = include 'x.php';
echo $z; // outputs 42

$y = include 'y.php';
echo $y; // ouputs 1, for 'true', because the include was successful
         // and the included file did not have a 'return' statement.

Also note that include will only execute the included code if it contains <?php ... ?> code block. Otherwise anything included is simply treated as output.

Marc B
  • 356,200
  • 43
  • 426
  • 500
6

Use file_get_contents or ob_get_clean, like so:

ob_start();
include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
$eyecatcher = ob_get_clean();
Jan Willem
  • 1,280
  • 1
  • 9
  • 9
  • Thank you very much I tried `file_get_contents` and it didn't output the way I wanted but then did your code with `ob_end_clean` and the output was exactly as I hoped for. So thank you very much I accept this answer as the one that solved my problem. – purple11111 Jul 07 '16 at 15:32
2

The following assigns the return value of include() to the variable $eyecatcher.

$eyecatcher = include ($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);

Because the include() was successful, it returns a boolean value of true, which is presented as "1" when you echo it.

If you wish to load the $eyecatcher variable with the contents of the file as a string, you do:

$eyecatcher = file_get_contents($_SERVER['DOCUMENT_ROOT'] . $jpath_eyecatcher_path);
Jef
  • 1,128
  • 9
  • 11