Dollar ($) sign in password string treated as variable
PHP is interpolating the variable $word
into the string mypas$word
, as is normal behaviour for string literals delineated with double quotes. Since $word
is presumably undefined, the resulting interpolated string is mypas
.
The solution is to use single quotes. Single-quoted string literals do not undergo variable interpolation.
The other answers all work until there are single quotes embedded in the passsword.
Fail:
$_DB['password'] = 'my'pas$word';
Alternatives:
If you don't have other escaped characters, you can escape the $ with \$
, e.g.
$_DB['password'] = "my'pas\$word";
Or it may be simpler to escape the single quote e.g.
$_DB['password'] = 'my\'pas$word';