4

How can I put '&' symbol to URL GET variable so it is part of string? The problem is it always split the string to next variable.

How can I make this work?

localhost/test.php?variable='jeans&shirts'    // so it executes it like a string

<?php

require "connect.php";

$variable = $_GET['variable'];

echo $variable;

?>

output is 'jeans'

instead of 'jeans&shirts'

1 Answer 1

10

You will want to urlencode() your string:

// Your link would look like this:
'localhost/test.php?variable='.urlencode('jeans&shirts');

When you want to use it, you would decode it:

echo $variable = urldecode($_GET['variable']);

ENCODE: http://php.net/manual/en/function.urlencode.php

DECODE: http://php.net/manual/en/function.urldecode.php


EDIT: To test write this:

echo $url = 'localhost/test.php?variable='.urlencode('jeans&shirts');
echo '<br />';
echo urldecode($url);

Your result would be:

// Encoded
localhost/test.php?variable=jeans%26shirts
// Decoded
localhost/test.php?variable=jeans&shirts
Sign up to request clarification or add additional context in comments.

5 Comments

This is misleading. You need to urlencode the string before using it in the url. Your 1st line of code should not reference $_GET['variable'] at all, and instead show perhaps a link tag or similar
@Steve Sorry, I was updating that when you commented because I knew it was a bit misleading
doesn't work for me :\ but never mind I did workaround by excluding the '&' symbol in URL
You should always use the urlencode() when turning a variable to query string. Should have made the $_GET['variable'] output to jeans%26shirts.
No problem hopefully you can get it to work! It will save you headaches in the future!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.