9

You can write PHP code like below in Laravel.

@php
    //
@endphp

Alternately you can write,

<?php
//
?>

What is the difference between about two method? What are the advantages?

For a example when we use to display something, we can use {{ $name}}. It can save developer time because if we use PHP we have to write <?php echo $name; ?>.

But what is the point of writing @php @endphp instead of <?php ?>

  • 1
    I don't think there is any extra benefit of using `@php @endphp` instead of ``. This is just to maintain consistency of blade syntex – Ahsan Sep 15 '17 at 07:04
  • Some people just prefer to use the `@php` versions so that it's the same as the rest of the blade directives. – Rwd Sep 15 '17 at 07:04
  • I think there is not much difference in using blade template syntax over php syntax, as per I know auto-escaping is difference in that. In blade template it auto escape string. – Nikita Sep 15 '17 at 07:11
  • You can save even more developer time writing =$name?>, but you wouldn't do that... If you write a blade.php file, you use blade. – Amarnasan Sep 15 '17 at 07:39
  • @Amarnasan That's a totally different story. If you have good PHP knowledge you should know that you can't use =$name?> if PHP short tag isn't enabled in the server. So, there is a clear disadvantage of writing ?=$name?>. – I am the Most Stupid Person Jun 12 '19 at 08:43

1 Answers1

11

Long in a short there is absolutely no difference.

Its just a directive that keep blade views consistent with other pre defined directives such as @while @if @foreach etc.

You can find definition under

Illuminate/view/Compilers/Concerns/CompilesRawPhp.php

simply it takes expression between <?php ? tags

protected function compilePhp($expression)
    {
        if ($expression) {
            return "<?php {$expression}; ?>";
        }
        return '@php';
    }
Anar Bayramov
  • 11,158
  • 5
  • 44
  • 64