PHP: file_get_contents() or simlar

Soldato
Joined
18 Oct 2002
Posts
9,047
Location
London
To make things easier to explain/understand I have 2 files similar to this:


PHP file A:
Code:
<?
  if (...) {
    echo x;
  } else {
    echo y;
  }
?>


PHP file B:
Code:
<?
  $output = file_get_contents(fileA.php);
  .
  .
  [do some things first !important]
  .
  .
  echo $output;
?>


Hopefully makes sense, but the trouble is $output has the echo of both x and y, not just one (as you might expect from and if statement!) I assume this is something to do with using the file_get_contents :confused:

EDIT: I just worked out it's not actually 'executing' PHP fileA! Is there a function that will grab the output of the file, and store it as my $output string?
 
Last edited:
KingAdora said:
I assume this is something to do with using the file_get_contents :confused:
Correct. You're trying to execute PHP code via file_get_contents(), which simply grabs the string data of the file. I don't see why you're not using a simple include in this situation as the files are local and to be executed - file_get_contents() is not for such a situation. So, in your case, fileA is being loaded, but not executed; so the if statements are not taking effect.

Give the code a rethink, you might want to put the fileA into a function with return values, assign the function to a variable, manipulate the return value and then output it as you're trying to do. Make sense?
 
You can execute it.. but include or require is much better. I fail to see any situations were you can't use it.

you can use eval($output); but you would need to strip all <?php ?> tags from it (I think)
 
Back
Top Bottom