ItSolutionStuff.com

How to Write PHP Code in Laravel Blade?

By Hardik Savani • May 14, 2024
Laravel

Hi Dev,

Now, let's see example of how to write php code in blade file in laravel. This article will give you simple example of how to write php code in laravel view. this example will help you how to write php code in laravel blade. We will look at example of laravel blade write php code.

Here, i will give you simple two way to write core php code in blade file in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application. first if using @php that provided by laravel and i will suggest that one and another is using <?php that is using php syntax.

So let's see both example one by one:

Example 1: @php

I will suggest this syntax to use php code:

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

@php

$title = "How to Write PHP Code in Laravel Blade? - ItSolutionStuff.com";

$myArray = ['id'=>1,'name'=>'Hardik'];

@endphp

<h1>{{ $title }}</h1>

</body>

</html>

Example 2:

<!DOCTYPE html>

<html>

<head>

<title></title>

</head>

<body>

<?php

$title = "How to Write PHP Code in Laravel Blade? - ItSolutionStuff.com";

$myArray = ['id'=>1,'name'=>'Hardik'];

?>

<h1>{{ $title }}</h1>

</body>

</html>

You can use anyone, i hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Bail Rule | Stop Validation On First Failure

Read Now →

How to Create Custom Helper Function in Laravel 7?

Read Now →

Laravel Disable Registration Route Example

Read Now →

Laravel Eloquent orWhere() Condition Example

Read Now →

How to use Soft Delete in Laravel?

Read Now →

How to use whereHas with orWhereHas in Laravel?

Read Now →

Laravel Eloquent Concat Two Columns Example

Read Now →