View Full Version : problem with the $_GET thingy

02-19-2008, 10:15 PM
hello people a newbie here with a simple question:

$code = $_GET['code'];
echo "$code"; returns abcd

$code = $_GET['code'];
echo "$code"; also returns abcd

how can i fix this problem. i know "&" is the problem so i want it to be ignored and treated as just another character.

thanks in advance

by the way, what other characters could potentially create the same problem?

02-19-2008, 10:28 PM
Write the & sign as & #38;

Just remove the space between the & and the # in the thing above (in cannot write it as it should be otherwise the browser actually converts it to a read & sign).

That 38 thing is the "html entity" for the & sign

php can do this automatically for you with the function urlencode

see http://www.php.net/manual/en/function.urlencode.php

So your link should be

index.php?code=abcd& #38;efgh

(once again just remove the space between & and # and it will work)

02-19-2008, 10:39 PM
And another small explanation: the & is a separator between the arguments in the $_GET request. Therefore, you should always use urlencoding (http://www.php.net/manual/en/function.urlencode.php) (like deamonlizard talks about).

02-19-2008, 10:47 PM
Examples of the above suggestions. This is how it will look when using
then you can decode it using urldecode.

$code = urlencode('abcd&efgh');
echo $code; // returns abcd%26efgh

$code = '';
$code = urldecode($_GET['code']);
echo $code; // returns abcd&efgh

02-19-2008, 10:48 PM
the & don't create problem, the problem is that you don't know that the query string must be one or more pair name=value separated by & or ;

in your second example if you use:

$code = $_GET['code'];
echo "$code"; also returns abcd // you don't need to quote $code, is a variable
$efgh = $_GET['efgh'];
echo $efgh; will return 'myvalue'

@jaap: urlencode is used to encode specials chars in url, not &

sorry, I missunderstand what you want. After _Aerospace_Eng_ I understand that you are talking about a single value which contain &.

@jaap: you are right in this case, & is special char inside a value, and urlencode work, :)

best regards

02-19-2008, 10:57 PM
If its a string that gets passed to the url then yes you can encode and yes it does encode &.