user1190478
user1190478

Reputation: 119

How to join variables in PHP

How can I join '<' and 'div>'?

When I echo

<?php echo "<"."div>";?>

it returns an empty string.

Upvotes: 2

Views: 93

Answers (5)

Secko
Secko

Reputation: 7716

Your code is executed and the '<' is joined with the 'div>', however there is nothing to show as you are trying to output a partial HTML tag in PHP. You can do this by means of entity code:

<   Less-than       &lt;
>   Greater-than    &gt;

In your case it looks like:

echo "&lt;"."div&gt;";

Other means of doing this is using PHP to add these entity codes for you:

htmlentities — convert all applicable characters to HTML entities

In code:

echo htmlentities('<div>');

Make sure you use echo, or otherwise you would end up with an empty string again.

Other means:

htmlspecialchars — convert special characters to HTML entities

In code:

echo htmlspecialchars('<div>');

Upvotes: 0

user2936213
user2936213

Reputation: 1011

Because it is a HTML Tag. You cannot print it like this.
Still if you want to print it you can see @egig's suggestion. You can use echo "&lt"."div&gt;"

Upvotes: 2

rccoros
rccoros

Reputation: 590

You can use htmlspecialchars for this.

<?php echo htmlspecialchars("<"."div>"); ?>

Upvotes: 0

ReNiSh AR
ReNiSh AR

Reputation: 2852

use:

htmlspecialchars("<a href='test'>Test</a>");

to display html tags in php

Upvotes: 0

user399666
user399666

Reputation: 19899

That's because you're echoing a HTML tag. View the source of the page.

Upvotes: 1

Related Questions