What is the difference between {{ $username }} and {!! $Username !!} In laravel?

Answer Posted / sachin-laradev

In Laravel, {{ $username }} and {!! $username !!} displays dynamic content within a Blade template. However, they have different behaviors depending on the context in which they are used.

{{ $username }} is used to display escaped output. This means that any special characters in the variable's value, such as HTML tags or JavaScript code, will be converted to their corresponding HTML entities to prevent them from being interpreted as code. This is done to help prevent cross-site scripting (XSS) attacks, where malicious code is injected into a web page.

{!! $username !!} is used to display unescaped output. This means the variable's value will be displayed exactly as it is, without any special characters being converted to HTML entities. This is useful when displaying HTML markup or other special characters.

However, using unescaped output can be risky, especially if the variable's value comes from user input. It can make your application vulnerable to XSS attacks. Therefore, you should always sanitize user input before displaying it on a web page and use the escaped output ({{ $variable }}) by default unless you have a good reason to use the unescaped output ({!! $variable !!}).

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain Constructor Injection ?

1076


Explain automatic injection in laravel?

550


How we can get user's detail when he is logged in using auth?

527


What are laravel facades?

573


What are the types of service providers?

499


Tell us what are advantages of laravel?

546


What is dd() function in laravel?

635


Tell me in which language it was written?

507


What is a laravel controller?

577


What is laravel echo?

571


What is laravel orm?

514


How do I seed my database for column that is a foreign key referencing to other table?

485


What is named route?

523


What are service providers?

528


Do you know what is php artisan. List out some artisan commands?

528