Bird
0
0

Which of the following is the correct way to include logic in a Laravel Blade template?

easy📝 Syntax Q3 of 15
Laravel - Views and Blade Templates
Which of the following is the correct way to include logic in a Laravel Blade template?
A<script>var total = price * quantity;</script>
B<?php $total = $price * $quantity; ?>
C@php $total = $price * $quantity; @endphp
D{{ $total = $price * $quantity }}
Step-by-Step Solution
Solution:
  1. Step 1: Identify Blade syntax for PHP code

    Blade uses @php ... @endphp to run PHP code inside templates.
  2. Step 2: Check other options for correctness

    is raw PHP tags not recommended in Blade; C is JavaScript, not PHP; D tries to assign inside output, which is invalid.
  3. Final Answer:

    @php $total = $price * $quantity; @endphp -> Option C
  4. Quick Check:

    Blade PHP block = @php $total = $price * $quantity; @endphp [OK]
Quick Trick: Use @php ... @endphp for PHP logic in Blade [OK]
Common Mistakes:
  • Using raw PHP tags in Blade
  • Mixing JavaScript with PHP logic
  • Trying to assign variables inside {{ }}

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes