Tyler Dusty
Tyler Dusty

Reputation: 13

PHP - Include inside Include (Not passing variables)

Lets say we have 3 files: File1.php, File2.php, File3.php

I want to have it where in File1.php I include() File2.php. Then in File2.php I include() File3.php. I tested it out by writing test text in File3.php, and when I viewed File1.php I could see that test text.

Now my problem is that if I make a variable, say '$test = "Success!";', in File3.php and then I try to call it in File1.php with 'echo $test;' it outputs nothing. It's not transferring the variables, but it is transferring standard text.

What's going on here?

(P.S. The reason I'm doing it like this is because of organization.)

EDIT

Here's my exact code: (also forgot to mention that I'm grabbing the url of the site first)

file3.php

 <?php
      $test = "Success!";

file2.php

 <?php
      include 'http://' . $_SERVER['SERVER_NAME'] . '/file3.php';

file1.php

 <?php
      include 'http://' . $_SERVER['SERVER_NAME'] . '/file2.php';
      echo $test;

Upvotes: 1

Views: 714

Answers (2)

M. Suleiman
M. Suleiman

Reputation: 848

echo "$test"; //with double quotes :)

// or echo $test;

Can you provide more code?

Per http://php.net/manual/en/function.include.php :

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

Check Example #1

Edit: Per http://php.net/manual/en/features.remote-files.php :

In addition, URLs can be used with the include(), include_once(), require() and require_once() statements (since PHP 5.2.0, allow_url_include must be enabled for these)

So, you cannot include files with the URL, instead, as stated in my comment, use local path. Unless you have allow_url_fopen enabled. This should work.

Upvotes: 3

Vili
Vili

Reputation: 1679

If your files have these lines, then echo would print out your message:

File1.php:

<?php
    include 'File2.php';
    echo $test;
?>

File2.php:

<?php
    include 'File3.php';
?>

File3.php:

<?php
    $test='success!';
?>

Upvotes: 0

Related Questions