How to pass a PHP variable using the URL

In your link.php your echo statement must be like this:

echo '<a href="https://stackoverflow.com/questions/5440197/pass.php?link=" . $a . '>Link 1</a>';
echo '<a href="https://stackoverflow.com/questions/5440197/pass.php?link=" . $b . '">Link 2</a>';

Then in your pass.php you cannot use $a because it was not initialized with your intended string value.

You can directly compare it to a string like this:

if($_GET['link'] == 'Link1')

Another way is to initialize the variable first to the same thing you did with link.php. And, a much better way is to include the $a and $b variables in a single PHP file, then include that in all pages where you are going to use those variables as Tim Cooper mention on his post. You can also include this in a session.

Leave a Comment