Search code examples
phpampersand

Combining '&' and 'currency_code' in PHP


I have a strange problem with PHP. Basically I was trying to do this:

$string = '&currency_code';

when I try to echo $string the output is:

¤cy_code

Has anyone encountered this? How can we get around this?


Solution

  • &curren is an HTML entity; browsers will render it as the ¤ symbol.

    Always run htmlspecialchars on any non-HTML text you want to output to avoid unexpected behavior like this.

    $string = "&currency_code";
    $escaped_string = htmlspecialchars($string);
    echo $escaped_string; // outputs the HTML "&currency_code", which
                          // appears to the user as "&currency_code".