php echo html标签所以它是可见的(不解释为代码)

I am currently trying to echo a text value from a variable which contains html-style tags. <...>

$string = "variable_name";
$tag_str = "<".$string.">";
echo $tag_str;

currently this echo's as nothing as it believes it is html code. How would I go about echoing <variable_name> to the page so it is viewable and not interpreted as code by the browser?

You'll have to html encode your output

$string = "variable_name";
$tag_str = "<".$string.">";
echo htmlspecialchars($tag_str);

The angle brackets (<>) are precisely what tells the browser that it should be treated as HTML code. Instead, output the HTML-encoded versions of those otherwise special characters:

$tag_str = "&lt;".$string."&gt;";

Alternatively, automate this process:

$tag_str = htmlspecialchars("<".$string.">");

Use highlight_string().See below code

$string = "variable_name";
$tag_str = "<".$string.">";
highlight_string($tag_str);