Reputation: 2811
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.
Upvotes: 1
Views: 301
Reputation: 76656
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
Upvotes: 6
Reputation: 3170
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
Upvotes: 1
Reputation: 37803
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]';
Upvotes: 1