Example 9: Passing variables in URLs

One way to pass variables between pages is to append them to an URL. The format for this is:

url?variable=value[&variable=value . . . ]

The variables passed become php variables with the same names. To pass variable1=hello and variable2=world to a page that prints two variables, I could use an request like:

http://mysite.com/mypage.php?variable1=hello&variable2=world

The page requested might have code like:

<?php
echo $variable1 ;
echo "<br>";
echo $variable2 ;
?>

The generated page will use the variable $variable1 to hold the value hello. This is the same as writing $variable1 = "hello" ; in the php. You can see this working in example-9a.php. (View the code: example-9a.php) To make the page say "hello world", I can call it as example-9a.php?variable1=hello&variable2=world.



© 1996 -- 2005, Noel Schutt
E-mail me at: ski ihatespam at schutt dot org
(hint: delete " ihatespam " , and replace " at " with "@" and " dot " with ".")
Page last updated: 2004.03.29