Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of writing @php @endphp instead of <?php ?> in Laravel Blade?

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 ?>

like image 371
I am the Most Stupid Person Avatar asked Sep 15 '17 06:09

I am the Most Stupid Person


People also ask

Can we write PHP code in blade template Laravel?

Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates. In fact, all Blade templates are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.

Why do we write blade in Laravel?

The Blade is a powerful templating engine in a Laravel framework. The blade allows to use the templating engine easily, and it makes the syntax writing very simple. The blade templating engine provides its own structure such as conditional statements and loops.

Why does Laravel use the blade template engine?

Laravel Blade template engine enables the developer to produce HTML based sleek designs and themes. All views in Laravel are usually built in the blade template. Blade engine is fast in rendering views because it caches the view until they are modified. All the files in resources/views have the extension .

What is __ In Laravel blade?

Laravel later introduced a great helper function __() which could be used for JSON based translations. For instance, in your blade files, {{ __('The Web Tier') }} Whereas “The Web Tier” is added in a JSON file inside of resources/lang directory i.e {locale}.json likeso, {


1 Answers

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';
    }
like image 64
Anar Bayramov Avatar answered Oct 30 '22 16:10

Anar Bayramov