user1051505
user1051505

Reputation: 962

How to avoid the '&' operator in strings

I have the following piece of code.

<?php
    $agentId = "LTH001";
    echo "&&" . $agentId;
?>

The output is &<H001 whereas I expected &&LTH001. I guess there is a conversion issue causing this output. Why do I get the unexpected output?

Upvotes: 2

Views: 108

Answers (2)

dvir
dvir

Reputation: 5115

You should use http://www.php.net/htmlspecialchars in order to avoid HTML converting of undesired strings:

echo htmlspecialchars("&&".$agentId);

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Your output is: &&LTH001.

Your browser is seeing the &LT and interpreting it as a character code (albeit a malformed one), and replacing it with <.

Please View Source or use htmlspecialchars next time ;)

Upvotes: 6

Related Questions