Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store $ in a PHP variable?

Tags:

php

I want to store a $ character in a PHP variable.

$var = "pas$wd";

I get the following error

Notice: Undefined variable: wd in C:\xxxxx  on line x

Help.

like image 211
Bruce Avatar asked Nov 29 '22 11:11

Bruce


1 Answers

You can use single-quoted strings :

$var = 'pas$wd';

This way, variables won't be interpolated.


Else, you can escape the $ sign, with a \ :

$var = "pas\$wd";


And, for the sake of completness, with PHP >= 5.3, you could also use the NOWDOC (single-quoted) syntax :

$var = <<<'STRING'
pas$wd
STRING;


As a reference, see the Strings page of the PHP manual (quoting a couple of sentences) :

Note: [...] variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

And :

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

like image 148
Pascal MARTIN Avatar answered Dec 18 '22 08:12

Pascal MARTIN