pasha
pasha

Reputation: 170

PHP passing variable to URL

I have a variable say, $CODE, and I want to include it into another variable which is actually an URL like: http://www.knmi.nl/klimatologie/uurgegevens/datafiles/275/uurgeg_275_2011-2020.zip so that would be:

$URL = "http://www.knmi.nl/klimatologie/uurgegevens/datafiles/275/uurgeg_275_2011-2020.zip"

I want to include $CODE variable into $URL instead of number "275" so that would be:

$URL = "http://www.knmi.nl/klimatologie/uurgegevens/datafiles/$CODE/uurgeg_$CODE_2011-2020.zip"

but the problem is the 2nd $CODE inclusion because of next underline ($CODE_) to it, and when I include quotations like '$CODE', the actual quotations will appear in the $URL variable so if the $CODE variable equals to number 275 it will be like:

$URL = "http://www.knmi.nl/klimatologie/uurgegevens/datafiles/'275'/uurgeg_'275'_2011-2020.zip"

which is not valid. could you tell me what is the problem?

Upvotes: 1

Views: 551

Answers (3)

Michael Berkowski
Michael Berkowski

Reputation: 270599

Surround it in {} as in

$URL = "http://www.knmi.nl/klimatologie/uurgegevens/datafiles/{$CODE}/uurgeg_{$CODE}_2011-2020.zip";

Inside a double-quoted string, it's often a good idea to surround variables in {} as an aid to the parser. It is necessary to surround complex variables like " a string with {$object->property[0]->value} inside, but not always necessary for simple variables. In your case, it must be done to isolae $CODE from the rest of the string beginning with _ so that it is recognizable as an existing variable.

This is explained in the PHP Strings documentation.

Upvotes: 3

Murray McDonald
Murray McDonald

Reputation: 631

Within double qotes you can use {$code} -- this will solve your problem with the following underbar

Upvotes: 0

Tchoupi
Tchoupi

Reputation: 14681

Use a concatenation operator:

$URL = "http://www.knmi.nl/klimatologie/uurgegevens/datafiles/".$CODE."/uurgeg_".$CODE."_2011-2020.zip"

Upvotes: 2

Related Questions