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 ?>
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.
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.
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 .
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, {
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';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With