Matt Belcaustier
Matt Belcaustier

Reputation: 39

PHP don't want to echo the characters "<" and ">"

I'm trying to echo this text :

echo $string = "Hello <username> !";

But what I get is Hello !, it removed my <username>.

Do you, please, know how to fix that ?

Upvotes: 0

Views: 308

Answers (5)

Ryan Tuosto
Ryan Tuosto

Reputation: 1951

When the < and > characters are rendered the browser interprets them as a new html tag. To prevent this from happening you should use the php htmlspecialchars() function to translate them into their html entity values ie &lt; and &gt;.

$username = htmlspecialchars("<username>");
echo $string = "Hello $username!";

http://php.net/manual/en/function.htmlspecialchars.php

Upvotes: 0

Anakin
Anakin

Reputation: 1291

Maybe, the php works fine.

But, when the HTML is interpreted in your browser, it goes to a username tag

Replace < and > with html entities should work.

$string = "Hello <username> !";
echo htmlentities($string);

Upvotes: 1

Md. Sahadat Hossain
Md. Sahadat Hossain

Reputation: 3236

replace < by &lt; and > by &gt;

Upvotes: 1

Spoody
Spoody

Reputation: 2882

Try this:

echo $string = "Hello &lt;username&gt; !";

This happened because your browser treated <username> as a HTML tag, You can use htmlspecialchars

PHP:htmlspecialchars

Upvotes: 1

Sahil Gulati
Sahil Gulati

Reputation: 15141

Try this hope this will help you out. Here < and > are the part of html entities.

<?php
ini_set('display_errors', 1);
$string = "Hello <username> !";
echo $string = htmlentities($string);

Output: Hello <username> !

Upvotes: 1

Related Questions