Home >Backend Development >PHP Tutorial >How do you pass multiple variables in a URL using PHP?

How do you pass multiple variables in a URL using PHP?

Susan Sarandon
Susan SarandonOriginal
2024-10-26 17:13:03618browse

How do you pass multiple variables in a URL using PHP?

Combining Multiple Variables in URL Parameters

When passing multiple variables to another page through the URL, it's crucial to concatenate them correctly to ensure they can be retrieved later. The ampersand character (&) plays a vital role in this process.

Concatenation using '&'

Suppose you're trying to pass both $email_address and $event_id in the URL like this:

<code class="php">$url = "http://localhost/main.php?email=" . $email_address . $event_id;</code>

Unfortunately, this method won't work. The $event_id variable will not be accessible on the next page. To fix this, use the following code instead:

<code class="php">$url = "http://localhost/main.php?email=$email_address&event_id=$event_id";</code>

By adding the ampersand (&) between the variables, you're essentially gluing them together. This ensures that they're treated as separate parameters.

Retrieving the Variables

On the next page, you can retrieve the individual variables using the $_GET superglobal:

<code class="php">if (isset($_GET['event_id'])) {
    $event_id = $_GET['event_id'];
}

if (isset($_GET['email'])) {
    $email_address = $_GET['email'];
}</code>

This method will successfully retrieve both $email_address and $event_id from the URL and allow you to use them in the next page.

The above is the detailed content of How do you pass multiple variables in a URL using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn