0

Please tell me what I am doing wrong, when the php file executes is saves the actual folder as "$name" instead of Peter. What am I doing wrong?

Here is my code:

$name = "Peter";
copy_directory('you/','dir/$name');
Mat
  • 202,337
  • 40
  • 393
  • 406
user1037871
  • 533
  • 1
  • 4
  • 6
  • 1
    You have to use double quotes instead of single quotes: "dir/$name" more infos: http://php.net/manual/en/language.types.string.php – Mimikry Jan 04 '12 at 18:59

5 Answers5

5

You'll need to use double quotes in order for the variable to be interpreted as Peter

copy_directory('you/',"dir/$name");
Jason MacLean
  • 513
  • 6
  • 15
4

You need to use double quotes if you want to expand variables within a string.

$name = "Peter";
copy_directory('you/',"dir/$name");

Or, alternately, concatenate the variable onto the string:

copy_directory('you/','dir/' . $name);
Justin ᚅᚔᚈᚄᚒᚔ
  • 15,081
  • 7
  • 52
  • 64
3

Use double quotes instead of single quotes;

$name = "Peter"; copy_directory('you/',"dir/$name"); 

Or alternatively, concatenate the variable;

$name = "Peter"; copy_directory('you/','dir/' . $name); 
siberiantiger
  • 305
  • 1
  • 3
  • 10
2

Problem is that you use the ' but should use there "" or 'dir/'. $name:

copy_directory('you/','dir/$name');
Tadeck
  • 132,510
  • 28
  • 152
  • 198
Risto Novik
  • 8,199
  • 9
  • 50
  • 66
2

It's good practice to use concatenation operator while using php variables.

copy_directory('you/','dir/'.$name);

Updated Answer:

This could be big debate what to use. It's everyone's own opinion. But people say we should avoid complexity of double quotes. Double quotes have memory save issues. It doesn't matter for small values. So I thought it's good practice to use concatenation operator while using php variables.

Community
  • 1
  • 1
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
  • 1
    According to **whom**? While I agree that concatenating variables is easier to read, it's quite subjective, and the fabled performance difference between single and double quotes has been proven to be [quite negligible and not entirely as assumed](http://phpbench.com/). – Justin ᚅᚔᚈᚄᚒᚔ Jan 04 '12 at 19:24
  • See the links in answer about parsing. – Somnath Muluk Jan 04 '12 at 19:36
  • Your first link reinforces my point, and the other two are five and seven years old, respectively. While past versions of PHP did exhibit a penalty when parsing double-quoted strings, the issue has been resolved for over three years. When variable parsing isn't being done, `'` vs `"` is a question of code style and readability (which are both subjective metrics). I agree with your preference, however I wouldn't want novice readers to see this argument and quote the misinformation as truth. – Justin ᚅᚔᚈᚄᚒᚔ Jan 04 '12 at 19:44
  • Users can also see links. Ist one is stackoverflow question. And experts given opinions over there. And no post is 5 or 7 years old. Other users are able to see it. I said it could be big debate. But it's good to listen to expert people. I am doing so. Thanks. – Somnath Muluk Jan 04 '12 at 19:55