faqts : Computers : Programming : Languages : PHP : Common Problems : Variables

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

77 of 111 people (69%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I use Global Variables? So they can be accessed between two php files?

Feb 8th, 2002 12:42
Michael Wales, Imran Aziz,


The only way to get a variable from one .php document to another is to 
require (or include) the .php document that contains the variable, and 
then you can access it as normal.
Eg:
FILE1.PHP
---------
<?
  require("file2.php");
  echo $var;
?>
FILE2.PHP
---------
<?
  $var = "value";
?>
You could also just pass the variables along with a URL extenstion like 
so:
http://www.domain.com/index.php?var1=value&var2=value&var3=value
So, say you made a Login form, and login.php is the file the form 
executed. You want to pass the username ($user) and password ($passwd) 
to nextpage.php with a link, you would do the following:
<?
  echo "<a href=\"nextpage.php?user=" . $user . "&passwd=" . $passwd . 
\">Go to Next Page</a>";
?>
The first variable you pass starts with a ?, all following must begin 
with a &, but this is just for IE. Don't try to recall them in PHP using 
the ? and &.
?>