Page 1 of 1

String operations (Error in PayPal sample scripts?)

Posted: Tue Jul 21, 2009 8:05 pm
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?)

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

Posted: Tue Jul 21, 2009 8:21 pm
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.)

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

Posted: Tue Jul 21, 2009 8:57 pm
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.)

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

Posted: Tue Jul 21, 2009 11:04 pm
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.