Laravel’s Blade templating engine simplifies the process of rendering views, but sometimes, you need to leverage the full capabilities of PHP within your Blade templates. In this comprehensive guide, we’ll explore how to write PHP code in Laravel Blade effectively and securely.
Example 1: @php
This syntax to use PHP code:
<!DOCTYPE html>
<html>
<head>
<title>Wizbrand</title>
</head>
<body>
@php
$title = "How to Write PHP Code in Laravel Blade - Wizbrand.com";
$myArray = ['id'=>1,'name'=>'Roshan'];
@endphp
<h1>{{ $title }}</h1>
</body>
</html>
Example 2:
<!DOCTYPE html>
<html>
<head>
<title>Wizbrand</title>
</head>
<body>
<?php
$title = "How to Write PHP Code in Laravel Blade - Wizbrand.com";
$myArray = ['id'=>1,'name'=>'Roshan'];
?>
<h1>{{ $title }}</h1>
</body>
</html>
I hope this will help you ….!!!!