I need HTML special characters in variable

I have a PHP variable and I need to use rich text formatting (like bold) but it is not possible with the code I have.

How could I do this?

For example:

My text:

<td>{{ $item->item_category->name ?? "Eliminaste esta Categoría" }}</td>

But I need:

<td>{{ $item->item_category->name ?? "<b>Eliminaste esta Categoría</b>" }}</td>

I tried the latest code but it doesn’t work.


Personally I’d recommend following Watercayman’s answer, but if you really want to do it your current way, you can use:

{!! $item->item_category->name ?? "<b>Eliminaste esta Categoría</b>" !!} 

as detailed in the documentation.

To explain the difference between the two:

{{ $var }} runs echo htmlspecialchars($var); which does several things, including converting <b> to &lt;b&gt;.

Running the alternative {!! $var !!} simply does echo $var.

The reason you might (should) use the first option is to sanitise user submitted data to prevent potential security risks. In your case, since it’s a hard coded string that you’ve written, this shouldn’t be an issue.