1

I have db with this string content:

[$3] [$ 3] [$3.2]

Need to echo it as it is.

When try to echo

echo "[$3] [$ 3] [$3.2]";

got this:

[] [$ 3] [.2]

Tried urlencode, htmlspecialchars, but didn't succeed.

How do I echo and get this?

[$3] [$ 3] [$3.2]

EDIT:

single quotes is not giving wanted result.

echo '[$3] [$ 3] [$3.2]';

[] [$ 3] [.2]

My php version is 5.2.14 and I am using Joomla.

EDIT2:

I figured out, the reason it's not working with single quotes is because of Joomla + Jumi. If I use pure php - it works ok.

ihtus
  • 2,673
  • 13
  • 40
  • 58
  • Are you sure you are using php? Can you show us how do you test your code? Are you using the command line like `php file.php` ? – Nico Sep 18 '13 at 21:06

3 Answers3

6

Use single quotes if you don't want the variable values to be interpolated.

From the PHP manual:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

echo '[$3] [$ 3] [$3.2]';

The quotes shouldn't matter for this particular case as PHP variables can't start with numbers.

For example:

echo '[$3] [$ 3] [$3.2]'; // single-quoted

will have the same effect as:

echo "[$3] [$ 3] [$3.2]"; // double-quoted

And both should output:

[$3] [$ 3] [$3.2]

But, for valid variables, the above rules apply.

Example:

$var = 'foo';
$string = 'this is a string with $var';
echo $string;

Output:

this is a string with $var

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • escape the $ with backslashes. `echo "[\$3] [\$ 3] [\$3.2]"; – XaxD Sep 18 '13 at 20:48
  • @ihtus: Yes, I have. See the demo. – Amal Murali Sep 18 '13 at 20:50
  • @XaxD: That's another way to do it, but it makes code look ugly, and can get tedious of there are a lot of `$`s to escape :) – Amal Murali Sep 18 '13 at 20:53
  • true, that's why i put out str_replace() to automate the process, but he says single quotes didn't fix the issue for him. – XaxD Sep 18 '13 at 20:54
  • Amal Murali: this is really weird, my php 5.2.14 is outputting [] [$ 3] [.2] even with single quotes.. – ihtus Sep 18 '13 at 21:01
  • @ihtus: if you check the [**demo here**](http://3v4l.org/etERo), you can see that it's working fine on all versions of PHP from 4.3.1 - 5.5.3. – Amal Murali Sep 18 '13 at 21:01
  • ok, plain php - works as should... the reason it is not working is because I am using Joomla. Sorry... – ihtus Sep 18 '13 at 21:07
1

Within double quotes, PHP is interpreting anything that starts with a $ as a variable, and trying to output the contents of $3.

When you don't want that behavior, simply use single quotes:

echo '[$3] [$ 3] [$3.2]';
VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
  • What's odd is that `$3` is not a valid variable name, and on my local install, PHP knows this. – Mark Sep 18 '13 at 20:50
1

A single-quoted string does not have variables within it interpreted. A double-quoted string does.

Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.

The single-quoted strings are faster at runtime because they do not need to be parsed.

Try this one

 echo '[$3] [$ 3] [$3.2]';

details here http://php.net/manual/en/language.types.string.php

Suvash sarker
  • 3,140
  • 1
  • 18
  • 21