Totalys
Totalys

Reputation: 465

How to pass "&" as part of an string in PHP?

How to pass "&" as part of an String in PHP?

Example:

str_test = "john&Sarah"

php_link = "www.test.com?names="+str_test

The result of this is:

www.test.com?names=John&Sarah

But php understand:

$GET_Names['names'] = John

I need it understand & as part of the name so

$GET_Names['names'] = john&Sarah

Is there a way to do that without replacing & and re-replacing it again later?

Upvotes: 0

Views: 317

Answers (3)

Daniel W.
Daniel W.

Reputation: 32270

Use existing functions exactly made for this purpose:

$str_test = urlencode("john&Sarah");

When you submit formdata, the browser automatically does encode the params. When you manually make a HTTP request, you need to manually form the data properly.

Sooner or later you will find out more characters that make your script behave unexpected, like a simple blankspace in a HTTP request, or a questionmark.

Upvotes: 3

Jake N
Jake N

Reputation: 10583

You seem lost. Here are some valid examples

// Assigned a string
$name = "john&Sarah";

// Assigned another string
$name = "john & Sarah";

//Get a name from query parameter
$name = $_GET['name'];

You need to be clearer with what you are trying to do, some of your code is invalid / makes no sense.

Upvotes: 0

Quentin
Quentin

Reputation: 943209

Use %26 to represent a & as data in a URL.

Upvotes: 1

Related Questions