m_junior
m_junior

Reputation: 591

php string concatenation "A<"."B" does not work

I'm writing a function to output HTML elements, the problem is: when I try to concatenate this two strings:

$tag = "<" . "tag";

The instruction echo $tag outputs nothing. What is wrong

Upvotes: 1

Views: 2111

Answers (2)

Yang
Yang

Reputation: 8701

As mentioned in comments, special characters like <, will be parsed by browser as HTML, therefore you won't see them as you expect.

Its almost the same thing:

$tag = 'p';

echo '<' . $tag '>' . Test . '</' . $tag . '>';

Which is the same as

echo '<p>' . Test . '</p>';

So after script execution you'll see just

Test

in a browser. but when viewing a source, it will be as

<p>Test</p>

If for some reason you want to see HTML tags, then you need to escape special chars using built-in function htmlentities().

In your case, you can just prepare a string, then just echo it like

echo htmlentities($string);

Upvotes: 2

Nazgul
Nazgul

Reputation: 1902

If by tag you mean an HTML entity then its not going to be seen in the browser. You may need to do a 'view source' to see what was created by echo call.

Upvotes: 0

Related Questions