Ayman
Ayman

Reputation: 131

how to fix variables starting with tags in php

I have the following code:

$name = "<test>";

$msg = "Hi $name Hope your feeling well today";

print_r ($msg);

The problem is, it will print Hi Hope your feeling well today and skip the $name

When I tried it this way:

$name = "<test>";

$msg = "Hi ".$name." Hope your feeling well today";

print_r ($msg);

it also printed the same.

When I tried it as:

$name = "<test>";

$msg = 'Hi $name Hope your feeling well today';

print_r ($msg);

it printed Hi $name Hope your feeling well today

I need a solution so that the variable $name will be printed as it is if starting with < or any other PHP related code.

Upvotes: 2

Views: 54

Answers (2)

Gul Muhammad Akbari
Gul Muhammad Akbari

Reputation: 260

Write instead of < &lt; and instead of > &gt;

Upvotes: 0

John Conde
John Conde

Reputation: 219874

View the source of your web page. It is there. You don't see it because it is in brackets which the browser interprets as HTML. Since HTML tags are interpreted and not displayed, you don't see that content.

To display those brackets, you need to convert them into HTML entities. You can use the aptly named htmlentities() function to do that.

$name = "<test>";
$msg = "Hi $name Hope your feeling well today";
echo htmlentities($msg, ENT_NOQUOTES, 'UTF-8');

Upvotes: 3

Related Questions