-1

I'm making a setup page where people can write their database login etc and that will then create a config.php file on their webserver with the info but obviously I can't just write

$file = fopen("config.php", "w");
fwrite($file, "$dbName = $dbName");

I actually tried

$file = fopen("config.php", "w");
fwrite($file, "$" . "dbName = $dbName");

But that doesn't work either.

Any way to store it as a PHP variable in a file I write using PHP?

sleeppyy
  • 55
  • 1
  • 2
  • 9
  • 5
    Use single quotes instead of double quotes – John Conde Aug 23 '16 at 19:28
  • `fwrite($file, '$dbName' . " = $dbName");` – Farkie Aug 23 '16 at 19:28
  • For reference, see [PHP:Strings](http://php.net/manual/en/language.types.string.php) and [...difference between single-quoted and double-quoted strings...](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – showdev Aug 23 '16 at 19:33
  • 1
    Either escape the dollar sign or use single quotes instead of double quotes. – Charlotte Dunois Aug 23 '16 at 19:34
  • See http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php – Gerard Roche Aug 23 '16 at 19:55

1 Answers1

3

As PHP Document says

If the string is enclosed in double-quotes ("), PHP will interpret the following escape sequences for special characters: \$ Dollar Sign

http://php.net/manual/en/language.types.string.php

So if you want to use $ in a double-quotes strings use \$ instead of $.

Or simply use single quotes

chris85
  • 23,846
  • 7
  • 34
  • 51
MDesign
  • 56
  • 6