1

This is an odd issue I ran into that adds a number (so far the number 1) to where the included data goes and positions the included data earlier in the code. I initially thought it might be some code on the TEXT.php that was causing the issue, but I simplified the two files until I got to a point where I have no idea what is causing the issue. The below code should result in the issue, despite how simple it is. While I can work around the issue, I would like to understand why I get a number and the included data moves.

The main code just references the Text.php in three ways.

  1. First way is how I normally include php files.
  2. Second is with some text before the echoed value that includes text, this results in a number appearing where the text should be and the text showing up earlier in the code.
  3. Third is just the echoed value that includes text, this results in a number appearing where the text should be and no text.

All that exists on the Text.php is sample text.

Sample Text

Below is the main code.

<?php
$Value = include 'Text.php';
include 'Text.php';
echo "<br>";
echo "Example: " .$Value;
echo "<br>";
echo $Value;
?>

The main code results in the following when viewed on a browser;

Sample TextSample Text
Example: 1
1

Instead of:

Sample Text
Example: Sample Text
Sample Text

I would appreciate if someone that knows code better than I could explain why this happens this way. Thank you for your time.

Trent
  • 25
  • 5
  • 3
    What **exactly** makes you think that the result should be different? – Nico Haase Mar 26 '21 at 22:26
  • 2
    Does https://stackoverflow.com/questions/5086695/php-include-prints-1/48742686 help? – Nico Haase Mar 26 '21 at 22:27
  • I would classify my PHP coding as a beginner level, I've only had one class and the rest of my knowledge is based on coding as I go, so largely it was that I didn't know better. – Trent Mar 29 '21 at 18:13

3 Answers3

4

The PHP manual page for include says:

Successful includes, unless overridden by the included file, return 1

So that explains where the 1 comes from.

For clarity: include executes the code in the included file. If that happens to involve an echo (or just some plain text outside a PHP block), then it echoes it, right there and then. That explains why you see "Sample Text" twice consecutively.

What include doesn't do is save the output and pass it back to the caller. If you want that then, in general, you should use a function with a return value, rather than an include.

N.B. You can use include with a file which has an inline return statement (see example #5 in the manual page), and it would work in the way you've tried above, capturing the return value. This is the scenario alluded to in the "unless overridden by the file" remark in the quotation above. But that's not what your current Text.php implementation is doing.

ADyson
  • 57,178
  • 14
  • 51
  • 63
3

The $Value variable is what's returned by php's include function. As per php's documentation, this function returns 1.

Reference: https://www.php.net/manual/en/function.include.php

In the case of your code and output.

<?php
$Value = include 'Text.php';  // This line sets $Value to 1 as the include is successful.
include 'Text.php'; // This line echos the contents of Text.php
echo "<br>";
echo "Example: " .$Value; // Echos $Value, which is set to 1 on line 1
echo "<br>";
echo $Value; // Echos $Value again, which is set to 1 on line 1
?>

Sample TextSample Text  <--- The text being included.
Example: 1  <--- The value of $Value is one, from a successful include
1  <--- The value of $Value, from a successful include
Ryan Kozak
  • 1,091
  • 6
  • 16
  • 21
  • Thank you, this helped lead me to an answer for my problem. Used a different approach but this got me on the right path. – Trent Apr 05 '21 at 23:12
1

to get the output you want

Sample Text
Example: Sample Text
Sample Text

you should type

<?php
$Value = file_get_contents('Text.php');//to get 'Sample Text' in Text.php file
//include 'Text.php';#you don't need to include it, since you get the content of it.
echo $Value."<br>";//Sample Text
echo "Example: " .$Value;//Example: Sample Text
echo "<br>";//Line Break
echo $Value;//Sample Text
?>
XMehdi01
  • 5,538
  • 2
  • 10
  • 34