Every time I try to assign an include/require to a variable, it adds a number at the end.
I would like to do it without that stupid number.
I tried file_get_contents but it does not work the same way.
If it’s not possible to do it with require, is there an EASY and SHORT (Single function and single line of code) way to do it with another function?
Please help me, because it’s driving me crazy.
For example:
FILE 1 (div.php):
<div><?php echo $x; ?></div>
FILE 2:
<?php
$x = "Example 1";
$file = include('div.php');
echo $file;
$x = "Example 2";
$file = include('div.php');
echo $file;
?>
OUTPUT:
Example 1
1
Example 2
1
This is because the default return value of
includeandrequireistrue, or1.When you include the file, it automatically outputs your content in the included file. When you
echo $file, you are echoing the return value of the include, which istrue.As a note, if you put
return false;orreturn "<div>$x</div>";in your included file, that would then become the value of$file. Whatever you return from your included file, that is passed to the variable.For example:
FILE 1 (monkey_do.php):
FILE 2 (main.php):