Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to assign or declare variable in laravel blade template

How to assign or declare variable in laravel blade template ?

Aman Jain, November 8, 2022March 16, 2024

Laravel blade is a powerful templating engine that gives you a lot of control over your HTML. One thing you can declare variable in laravel blade template easily by using different methds. This can be useful if you want to reuse a piece of data in your template, or if you want to make your template more dynamic. To declare a variable in a blade template, you use the “@php” and “@endphp” symbol and can define the variable as normal .

Declare variable in laravel blade template using @php syntax

For example, to declare a variable called “name”, you would use the following code to declare variable in laravel html

@php
$name = "John"
@endphp

You can then use this variable anywhere in your template by referencing it. For example, to print the value of the “name” variable, you would use the following code: {{ $name }} You can also use blade variables in place of HTML attributes. Above we easily declared and change variable value in blade template laravel

What is a Variable?

A variable is a value that can be changed. In programming, variables are used to store data values. In the Laravel Blade template system, variables are used to store data that can be displayed in the template.

Also Read : How to get laravel application root path ?

How to Set a Variable in a Laravel Blade Template

When working with Blade templates, it’s often necessary to declare variables to make information available to the template. There is two ways to Declare variable in laravel blade template:

Method 1: Laravel blade template @php syntax

In this method we will use @php and @endphp tags to set the variable as follow

@php
$name = "John"
@endphp

Method 2: Php tags to declare the variables

In this method we will use <?php and ?> tags to set the variable as follow

<?php 
$name = "John"
?>

Related

Php Laravel Laravel 9 bladedeclarelaravelvariable

Post navigation

Previous post
Next post

Related Posts

Php How to Change Date Format in Blade View File in Laravel

How to Change Date Format in Blade View File in Laravel ?

September 8, 2022March 16, 2024

Laravel default shows the date in the way its stored in database bu sometimes we want to change date format in blade view file in laravel according to our requirements. It can be multiple possibilities and requirement from client or product manager to change the date format so in this…

Read More

How to increase maximum file upload size in php ?

August 24, 2021August 24, 2021

Php is the most used language in web. sometimes it required to upload the files which is greater then 2 mb. In php default upload size is 2 mb and if we wanted to increase it then we have multiple ways to change the size of uploading. 1. Change from…

Read More
Php Google Map with Multiple Marker and Info Box in Laravel

Google Map with Multiple Marker and Info Box in Laravel

August 30, 2022March 16, 2024

In this article we will learn to integrate google map with multiple marker and info box in laravel. Google maps are used to navigate the user location or indicate the location of business and any user. Sometimes In our application we want to show the multiple location with some information…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version