I might be missing something pretty simple here...but we'll find out I guess.

I have a PHP page, that generates a variable

i.e. $var = xyz

This page also has code on it that uses a ROTATING script that pulls text files from another directory and displays them at the appropriate places on my page where the code is inserted.

What I want is for my $var to be available to the TEXT files that are included and rotated with my rotating script.

$Var is used in the text files as a word in a paragraph, right now I get nothing so I know my echo "$var"; is working in my text file but the variable is empty. How can I get access to that variable?

I thought sessions might do it for me but they don't seem to be doing the trick.

Ideas? Tips? 😕

    You might be a victim of variable scope here.

    Lets look at an example:

    Main Script

    
    <?php 
    
    $var = "hello there!";
    
    include('somefile.php');
    
    ?>
    
    

    Include file:

     
    
    This is a piece of text   that is <strong>simple</strong> HTML<br>
    Now we want to insert  the value into the next line<br>
    This is the value: <?php echo $var; ?><br>
    
    

    The above should work. When you include a file using php it merely inserts the file in line and then parses it. So it is as if the whole files is

    
    <?php 
    
    $var = "hello there!";
    
    ?>
    
    This is a piece of text that is <strong>simple</strong> HTML<br>
    Now we want to insert  the value into the next line<br>
    This is the value: <?php echo $var; ?><br>
    
    

    If this is not happening for you, then check your script tags they may be mismatched. Also, remeber that a varibale is only available within the scop it is declared. So if you try to use it within a function in the include file you need to declare it as global.

      Thx Subwayman,

      Looking into the SCOPE took care of my problem, I had to declare my variable as a global inside the rotating function for it to work properly!

        Write a Reply...