String operations (Error in PayPal sample scripts?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steve9876
Forum Newbie
Posts: 13
Joined: Tue Jul 21, 2009 7:34 pm

String operations (Error in PayPal sample scripts?)

Post by steve9876 »

I'm a PHP "newbie" and planning to use it to set up an ecommerce website with payment through PayPal.

After the customer pays, PayPal will send me a message("IPN" or "PDT") and requires that I acknowledge it. PayPal provides sample scripts for this purpose (e.g., https://www.paypaltech.com/SG2/). The code to take the incoming message and construct the response includes
$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
Wouldn't the result of this be "cmd=_notify-validate&$key=$value&$key=$value&$key=$value..."?

Shouldn't that obviously be $req .= &$key . "=" . $value? Or will the code as given somehow give the desired result in PHP?

(Also, can you give me a quick review of what &$key does?)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: String operations (Error in PayPal sample scripts?)

Post by requinix »

Code: Select all

"&$key = $value"
That's in double-quotes. It means PHP will automatically stick $key and $value into the right spots.
It is identical to saying

Code: Select all

"&" . $key . " = " . $value
By the way, the & does not mean anything to PHP here. It's part of the string - for the URL.

(However PHP does have a & operator - it's for references.)
steve9876
Forum Newbie
Posts: 13
Joined: Tue Jul 21, 2009 7:34 pm

Re: String operations (Error in PayPal sample scripts?)

Post by steve9876 »

Thanks. I'm glad I checked here before I embarrassed myself on a PayPal forum.

(In my defense, this is a highly idiosyncratic and rather obscure usage. In virtually every other language, single quotes and double quotes are interchangable.)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: String operations (Error in PayPal sample scripts?)

Post by requinix »

steve9876 wrote:(In my defense, this is a highly idiosyncratic and rather obscure usage. In virtually every other language, single quotes and double quotes are interchangable.)
Done C much?

Single- and double-quoted strings are mostly interchangeable in PHP. It's when you start putting variables into it that it can get tricky. Check out the documentation.
Post Reply