PHP code inside a Laravel 5 blade template PHP code inside a Laravel 5 blade template php php

PHP code inside a Laravel 5 blade template


According to documentation, in Laravel 5.2 and newer you can use the following code:

@php{{-- PHP code here --}}@endphp

Alternatively, you can extend the Blade templating engine as it's described here.

If neither of the above solutions is suitable, you are stuck with the answers given by Armen and by Gonzalo.


Just open and close PHP tags:

<?php $style = '...'; ?>


Laravel recipes suggest a simple, but effective, way to do it without including the PHP tags:

{{--*/ $var = 'test' /*--}}

{{-- --}} works as a blade comment / and / reverts the effect of comment resulting on

<?php $var = 'test' ?>

The problem is that is longer than including PHP tags :-(