How to Write PHP Code in Laravel Blade?

Posted by

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 ….!!!!

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x