W3docs

Passing multiple variables to another page in url

There are several ways to pass variables from one PHP page to another.

There are several ways to pass variables from one PHP page to another:

  1. Using the $_GET superglobal array:

How to pass variables from one PHP page to another?

<?php

$name = 'John';
$age = 30;

// Pass variables in the URL
echo "<a href='page2.php?name=$name&age=$age'>Go to page 2</a>";

On page 2, you can access the variables like this:

How to access variables from one PHP page to another?

<?php

$name = $_GET['name'];
$age = $_GET['age'];

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

  1. Using the $_POST superglobal array:

Example of using $_POST superglobal array in PHP

<?php

$name = 'John';
$age = 30;

// Use a form to pass variables to page2.php
echo "<form method='post' action='page2.php'>
      <input type='hidden' name='name' value='$name'>
      <input type='hidden' name='age' value='$age'>
      <input type='submit' value='Go to page 2'>
      </form>";

On page 2, you can access the variables like this:

Example of using $_POST superglobal array to access variables in PHP

<?php

$name = $_POST['name'];
$age = $_POST['age'];
  1. Using a session:

Example of using a session in PHP

<?php

// Start the session on page 1
session_start();

$name = 'John';
$age = 30;

// Store variables in the session
$_SESSION['name'] = $name;
$_SESSION['age'] = $age;

// Redirect to page 2
header('Location: page2.php');

On page 2, you can access the variables like this:

Example of using a session to access variables in PHP

<?php

// Start the session on page 2
session_start();

$name = $_SESSION['name'];
$age = $_SESSION['age'];