-3

Possible Duplicate:
Difference between single quote and double quote string in php

I have some strings which which are made up of random characters including $ sign, this becomes a problem if the string happens to begin with $ as it is treated as a variable, is there some way around this problem?

<?php
$variable="$&%51a-notaVariable";
?>
Community
  • 1
  • 1
user1559811
  • 437
  • 3
  • 10
  • 26

3 Answers3

4

Use single quotes:

<?php
   $variable='$&%51a-notaVariable';
?>
Stefan Neubert
  • 1,053
  • 8
  • 22
2

Answer 1: Use single quotes instead of double quotes.

$variable = '$&%51a-notaVariable';

Answer 2: If you definitely need to use double quotes for whatever reason, you can escape the dollar sign with a backslash.

$variable = "\$&%51a-notaVariable";
SDC
  • 14,192
  • 2
  • 35
  • 48
  • Thanks, yeah I did need to use double quotes so I will use \ . Thanks – user1559811 Jan 04 '13 at 12:48
  • As you are able to concat Strings and vars, there's no need to use " or ' exclusively. I like ' more, because escaped Strings (especially HTML code with attributes) is ugly to read, but there are arguments for both kinds of quotes. – Stefan Neubert Jan 04 '13 at 12:51
1

Short answer - yes. Use ' instead of " as it won't try to parse the characters in the string. :)

Long answer - yes. Note that you'll have to concat strings instead of putting variables "$inline" (or use sprintf() & co)... however, all in all, it's a beneficial tradeoff.

karllindmark
  • 6,031
  • 1
  • 26
  • 41