Bir PHP değişkeni içinde $ depolamak nasıl?

3 Cevap

Ben bir PHP değişkeni bir $ karakteri saklamak istiyorum.

$var = "pas$wd";

Ben şu hatayı alıyorum

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

Yardım.

3 Cevap

Eğer tek tırnakla kullanabilirsiniz:

$var = 'pas$wd';

Bu şekilde, değişkenler interpolasyon olmayacaktır.


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.

Ve:

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

Tek tırnak genişleme inhibe:

var = 'pas$wd';

Çift tırnak değişken interpolasyon etkinleştirin. Çift tırnak kullanıyveyasanız Yani, $ else değişken enterpolasyon yapmak değil tek tırnak kullanabilirsiniz kaçmak gerekir.

$ var = "pas\$wd";

veya

$ var = 'pas$wd';