1

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?

0

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@user945782 What do you mean `` for a variable?
actually I am going to run a command like ` wget http://... ` which a variable ($CODE) need to be included in the URL but I don't know how to include that when ` ` is present, thanks
2

Use a concatenation operator:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.