Search code examples
phpfopenfwrite

PHP adding $ signs into strings without it being a variable?


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?


Solution

  • 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