Page 1 of 1

How to use a variable on multiple seperate pages?

Posted: Fri Jan 15, 2010 2:36 pm
by ShadowIce
How would I use this code to allow for me to use the same variable defined on the 1st page, on 2 or more seperate pages?

for instance, I wanted to print $foo on page 2, or page 4, but it is showing up as not there at all when I echo it.

test.php:

Code: Select all

<?php
function test() {
    $foo = "local variable";
 
    echo '$foo in global scope: ' . $GLOBALS["foo"] . "<br>\r\n";
    echo '$foo in current scope: ' . $foo . "<br>\r\n";
}
 
$foo = "Example content";
test();
?>
 
<form name="test" id="test" action="./vartest.php" method="POST">
 
<input type="submit" value="Submit">
 
</form>
vartest.php:

Code: Select all

<?php
    echo '$foo in global scope: ' . $GLOBALS["foo"] . "<br>\r\n";
    echo '$foo in current scope: ' . $foo . "<br>\r\n";
?>


The variables do NOT have to be in a function either.

Infact, I'd prefer if they weren't.

Thanks!

ShadowIce~

Re: How to use a variable on multiple seperate pages?

Posted: Fri Jan 15, 2010 3:09 pm
by requinix
Define $foo in one file, then include or require that file in other pages.

Code: Select all

<?php // foo.php
 
$foo = "some value";

Code: Select all

<?php
 
include "foo.php";
echo $foo;

Re: How to use a variable on multiple seperate pages?

Posted: Fri Jan 15, 2010 3:17 pm
by ShadowIce
What if it is a $_POST[] in the 1st page? THAT's what I'm after

Re: How to use a variable on multiple seperate pages?

Posted: Fri Jan 15, 2010 3:45 pm
by Jonah Bron
$_SESSION is what you're looking for.