Reputation: 471
I would like to create a variable in one php page and then use it another php page, is there any way this can be done?
Upvotes: 0
Views: 298
Reputation: 13186
You need a session variable. Make it like this:
$_SESSION['my_var'] = 'your_value';
Then, you just need to use $_SESSION['my_var']
everywhere you need it as a global variable.
Pay attention that you have to put session_start();
in the first line of your file codes everywhere you want to work with PHP sessions.
Upvotes: 0
Reputation: 219087
There are many ways to do this, depending on your setup and your needs.
<a href="otherpage.php?value=<?php echo urlencode($someValue) ?>">click here</a>
This would allow you to pass the value along to the next page and then completely forget about it after it's been used, if you don't need to keep it around for anything else.As with many things, you have many options.
Upvotes: 1
Reputation: 143
Use session variables.
First of all every page you want to use sessions on needs to have the function session_start(); declared at the top of every page. You then can use the session superglobal to store things.
Page1.php:
<?php
session_start();
$_SESSION['variable_name'] = 'some string';
?>
Page2.php:
<?php
session_start();
echo $_SESSION['variable_name'];
?>
Page2.php will show: some string
You can store strings, ints, arrays etc. Good Luck.
Upvotes: 1
Reputation: 198247
If you're concerned about linking two PHP pages, you can just add it to a link:
one.php:
<a href="two.php?variable=<?php echo rawurlencode($variable);>"link</a>
two.php:
<?php echo htmlspecialchars($_GET['variable']); ?>
See Variables From External SourcesDocs and $_GET
Docs.
Upvotes: 0
Reputation: 870
you can put something in session variable, which can be accessed on any page
Upvotes: 0